SQL

Q1: What is SQL?

Answer:
SQL (Structured Query Language) is a standard programming language used to create, manage, manipulate, and retrieve data from relational databases. It is widely used for performing operations such as querying, inserting, updating, and deleting data.


Q2: What is a Database?

Answer:
A database is an organized collection of structured data that is stored electronically. It allows users to efficiently store, retrieve, update, and manage information.


Q3: What are the different types of SQL statements?

Answer:
SQL statements are categorized into the following types:

  • DDL (Data Definition Language): CREATE, ALTER, DROP, TRUNCATE
  • DML (Data Manipulation Language): INSERT, UPDATE, DELETE
  • DQL (Data Query Language): SELECT
  • DCL (Data Control Language): GRANT, REVOKE
  • TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT

Q4: What is a Primary Key?

Answer:
A Primary Key is a column (or combination of columns) that uniquely identifies each record in a table. It cannot contain NULL values and duplicate values are not allowed.


Q5: What is a Foreign Key?

Answer:
A Foreign Key is a column in one table that references the Primary Key of another table. It establishes a relationship between two tables and maintains referential integrity.


Q6: What is Normalization?

Answer:
Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. It divides large tables into smaller related tables using relationships.

Common Normal Forms:

  • First Normal Form (1NF)
  • Second Normal Form (2NF)
  • Third Normal Form (3NF)
  • Boyce-Codd Normal Form (BCNF)

Q7: What is the difference between SQL and MySQL?

Answer:

SQLMySQL
SQL is a language used to interact with databases.MySQL is an open-source Relational Database Management System (RDBMS).
It defines database operations.It uses SQL to manage databases.
SQL is a standard language.MySQL is software developed by Oracle.

Q8: What is a JOIN?

Answer:
A JOIN is used to retrieve data from two or more related tables based on a common column.


Q9: What are the different types of JOINs?

Answer:

  • INNER JOIN: Returns matching records from both tables.
  • LEFT JOIN: Returns all records from the left table and matching records from the right table.
  • RIGHT JOIN: Returns all records from the right table and matching records from the left table.
  • FULL OUTER JOIN: Returns all matching and non-matching records from both tables.
  • CROSS JOIN: Returns the Cartesian product of two tables.
  • SELF JOIN: Joins a table with itself.

Q10: What is the difference between Primary Key and Unique Key?

Answer:

Primary KeyUnique Key
Only one Primary Key per table.Multiple Unique Keys are allowed.
Cannot contain NULL values.Can contain one NULL value (DBMS dependent).
Uniquely identifies each row.Ensures uniqueness but is not the main identifier.

Q11: What is a Subquery?

Answer:
A Subquery is a query written inside another SQL query. It is used to retrieve intermediate results that are used by the outer query.


Q12: What is the difference between DELETE and TRUNCATE?

Answer:

DELETETRUNCATE
Removes selected rows.Removes all rows.
WHERE clause can be used.WHERE clause cannot be used.
Can be rolled back (if transaction supported).Usually cannot be rolled back after commit.
Slower.Faster because it deallocates pages.

Q13: What is a View?

Answer:
A View is a virtual table created from the result of an SQL query. It does not store data itself but displays data from one or more tables.


Q14: What is a Stored Procedure?

Answer:
A Stored Procedure is a precompiled collection of SQL statements stored in the database. It can be executed repeatedly and helps improve performance and code reusability.


Q15: What is the difference between UNION and UNION ALL?

Answer:

UNIONUNION ALL
Removes duplicate records.Includes duplicate records.
Slightly slower.Faster because duplicates are not removed.

Q16: What is a Transaction?

Answer:
A Transaction is a sequence of SQL operations executed as a single unit of work. It ensures that either all operations are completed successfully or none are applied.

Transaction Commands:

  • COMMIT
  • ROLLBACK
  • SAVEPOINT

Q17: What is ACID in SQL?

Answer:
ACID properties ensure reliable database transactions.

  • Atomicity: All operations succeed or fail together.
  • Consistency: Database remains valid before and after a transaction.
  • Isolation: Transactions do not interfere with each other.
  • Durability: Committed data remains permanently stored.

Q18: What is the difference between CHAR and VARCHAR?

Answer:

CHARVARCHAR
Fixed-length data type.Variable-length data type.
Faster retrieval.Saves storage space.
Wastes space if data is shorter.Uses only required storage.

Q19: What is the difference between Clustered and Non-Clustered Index?

Answer:

Clustered IndexNon-Clustered Index
Stores data physically in sorted order.Stores pointers to data.
Only one per table.Multiple allowed per table.
Faster for range queries.Faster for specific lookups.

Q20: What is a Constraint in SQL?

Answer:
Constraints are rules applied to table columns to ensure data integrity.

Common constraints include:

  • PRIMARY KEY
  • FOREIGN KEY
  • UNIQUE
  • NOT NULL
  • CHECK
  • DEFAULT

Q21: What is the HAVING clause used for?

Answer:
The HAVING clause filters grouped data after the GROUP BY clause. Unlike WHERE, it works with aggregate functions.


Q22: What is the difference between a Stored Procedure and a Function?

Answer:

Stored ProcedureFunction
Can perform database operations.Mainly returns a value.
May return multiple values.Returns a single value.
Can contain transactions.Cannot perform transaction control.

Q23: What is the difference between Single-row and Multi-row Functions?

Answer:

  • Single-row functions operate on individual rows and return one result per row.
  • Multi-row functions (Aggregate functions) operate on multiple rows and return a single result.

Examples:

  • Single-row: UPPER(), LOWER(), LENGTH()
  • Multi-row: COUNT(), SUM(), AVG(), MAX(), MIN()

Q24: What is the difference between MINUS and EXCEPT?

Answer:

  • MINUS is supported in Oracle.
  • EXCEPT is supported in SQL Server and PostgreSQL.

Both return rows from the first query that are not present in the second query.


Q25: What is the difference between UNION and JOIN?

Answer:

UNIONJOIN
Combines rows from multiple SELECT statements.Combines columns from related tables.
Number of columns must match.Uses relationships between tables.

Q26: What is the difference between Database and Schema?

Answer:

DatabaseSchema
Collection of related data.Logical container for database objects.
Can contain multiple schemas.Contains tables, views, procedures, etc.

Q27: What is the difference between EXISTS and IN?

Answer:

EXISTSIN
Checks if subquery returns rows.Checks if a value exists in a list.
Faster for large datasets.Better for small datasets.

Q28: What is a Self Join?

Answer:
A Self Join joins a table with itself using aliases. It is commonly used to compare rows within the same table.


Q29: What is the purpose of the GROUP BY clause?

Answer:
The GROUP BY clause groups rows with the same values into summary rows. It is commonly used with aggregate functions like COUNT(), SUM(), AVG(), MAX(), and MIN().


Q30: What is the difference between WHERE and HAVING?

Answer:

WHEREHAVING
Filters rows before grouping.Filters groups after grouping.
Cannot use aggregate functions.Can use aggregate functions.

Q31: What is the difference between a Correlated and Non-Correlated Subquery?

Answer:

  • Correlated Subquery: Depends on the outer query and executes once for each row.
  • Non-Correlated Subquery: Executes independently and returns results to the outer query.

Q32: What is the difference between a Temporary Table and a Table Variable?

Answer:

Temporary TableTable Variable
Stored in TempDB.Stored mainly in memory.
Better for large datasets.Suitable for small datasets.
Can have indexes.Limited indexing support.

Q33: What is the difference between FULL OUTER JOIN and CROSS JOIN?

Answer:

FULL OUTER JOINCROSS JOIN
Returns matched and unmatched rows.Returns Cartesian product of both tables.
Uses matching condition.No matching condition required.

Q34: What is the CASE statement in SQL?

Answer:
The CASE statement performs conditional logic within SQL queries. It works like an IF-ELSE statement.


Q35: What is the difference between Candidate Key and Composite Key?

Answer:

Candidate KeyComposite Key
A single or minimal set of columns that uniquely identifies rows.Combination of two or more columns used as a unique key.

Q36: What is the difference between LIKE and IN?

Answer:

LIKEIN
Used for pattern matching using % and _.Used to compare values against a list of values.
Best for searching text patterns.Best for exact value matching.