Comprehensive SQL Questions and Answers (Beginner to Intermediate)
Basic SQL
Q: What is SQL and why is it used?
A: SQL (Structured Query Language) is used to interact with relational databases. It allows performing tasks
such as querying, updating, and managing data.
Q: What are the different types of SQL statements?
A: The main types are:
- DDL (Data Definition Language): CREATE, ALTER, DROP
- DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
- DCL (Data Control Language): GRANT, REVOKE
- TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT
Q: How do you create a table in SQL? Give an example.
A: Example:
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(100),
Salary DECIMAL(10, 2)
);
Q: What is a primary key?
A: A primary key uniquely identifies each record in a table. It cannot be NULL or have duplicate values.
Q: What is a foreign key?
A: A foreign key is a column in one table that refers to the primary key in another table, creating a relationship
between the tables.
Q: How do you insert data into a table?
A: Example:
INSERT INTO Employees (ID, Name, Salary)
VALUES (1, 'John Doe', 50000.00);
Q: What is the difference between WHERE and HAVING clause?
A: WHERE filters rows before grouping, while HAVING filters groups after aggregation.
Q: How do you update existing records in a table?
A: Example:
UPDATE Employees SET Salary = 60000.00 WHERE ID = 1;
Q: How do you delete records from a table?
A: Example:
DELETE FROM Employees WHERE ID = 1;
Q: What is the use of the SELECT statement?
A: SELECT is used to retrieve data from one or more tables.
Example: SELECT Name, Salary FROM Employees;
SQL Functions
Q: What are aggregate functions in SQL? Give examples.
A: Aggregate functions perform calculations on multiple rows and return a single result.
Examples: COUNT(), SUM(), AVG(), MAX(), MIN().
Q: What is the difference between COUNT(*) and COUNT(column_name)?
A: COUNT(*) counts all rows, while COUNT(column_name) counts non-NULL values in a specific column.
Q: Explain the use of GROUP BY clause with an example.
A: GROUP BY groups rows sharing a property.
Example:
SELECT Department, AVG(Salary) FROM Employees GROUP BY Department;
Q: What are scalar functions? Give examples.
A: Scalar functions operate on individual values and return a single value.
Examples: UPPER(), LOWER(), LEN(), ROUND().
Q: What is the difference between CHAR_LENGTH() and LENGTH()?
A: CHAR_LENGTH() returns the number of characters, while LENGTH() returns the number of bytes.
Joins and Subqueries
Q: What are the different types of joins in SQL?
A: Types include INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN, and SELF
JOIN.
Q: Explain INNER JOIN with an example.
A: Example:
SELECT E.Name, D.DeptName
FROM Employees E
INNER JOIN Departments D ON E.DeptID = D.ID;
Q: What is the difference between LEFT JOIN and RIGHT JOIN?
A: LEFT JOIN returns all records from the left table and matching ones from the right table, while RIGHT
JOIN does the opposite.
Q: What is a FULL OUTER JOIN?
A: It returns all records when there is a match in either left or right table, filling NULL where no match exists.
Q: What are subqueries? Provide an example.
A: A subquery is a query inside another query.
Example:
SELECT * FROM Employees WHERE Salary > (SELECT AVG(Salary) FROM Employees);
Q: What is the difference between correlated and non-correlated subqueries?
A: A correlated subquery depends on the outer query for its values, while a non-correlated subquery can run
independently.
Advanced Queries
Q: What is a view in SQL? How do you create one?
A: A view is a virtual table created from a query.
Example:
CREATE VIEW HighSalary AS SELECT * FROM Employees WHERE Salary > 60000;
Q: What is an index? How does it improve query performance?
A: An index is a database object that speeds up data retrieval.
Example:
CREATE INDEX idx_name ON Employees(Name);
Q: What is normalization? Explain different normal forms.
A: Normalization organizes data to reduce redundancy and improve data integrity.
Normal forms:
- 1NF: Atomic values
- 2NF: No partial dependency
- 3NF: No transitive dependency
Q: What is denormalization and when is it used?
A: Denormalization combines tables to improve read performance, often at the cost of write performance.
Q: Explain the concept of transactions in SQL.
A: A transaction is a sequence of operations performed as a single logical unit of work. It can be committed or
rolled back.
Q: What are ACID properties in a database transaction?
A: ACID: Atomicity, Consistency, Isolation, Durability.
Q: How do you use CASE statements in SQL?
A: Example:
SELECT Name, CASE WHEN Salary > 50000 THEN 'High' ELSE 'Low' END AS SalaryLevel FROM
Employees;
Q: What is a CTE (Common Table Expression)?
A: CTE is a temporary result set used in a query.
Example:
WITH DeptCTE AS (SELECT DeptID, AVG(Salary) AS AvgSal FROM Employees GROUP BY DeptID)
SELECT * FROM DeptCTE;