SQL Questions and Answers
SQL Interview Questions and Answers
1. Basic SQL Questions
Q1: What is SQL?
-
Retrieve data (
SELECT) -
Insert, update, and delete data (
INSERT,UPDATE,DELETE) -
Define and modify database structures (
CREATE,ALTER,DROP) -
Control access and permissions (
GRANT,REVOKE)
Q2: What are the different types of SQL commands?
-
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
Q3: What is the difference between WHERE and HAVING?
Answer:
-
WHEREfilters rows before aggregation. -
HAVINGfilters groups after aggregation.
-- WHERE exampleSELECT * FROM employeesWHERE salary > 50000;-- HAVING exampleSELECT department_id, AVG(salary)FROM employeesGROUP BY department_idHAVING AVG(salary) > 60000;Q4: What are the types of joins in SQL?
Answer:
INNER JOIN – Returns matching rows only
LEFT JOIN (LEFT OUTER JOIN) – Returns all rows from the left table + matching rows from the right
RIGHT JOIN (RIGHT OUTER JOIN) – Returns all rows from the right table + matching rows from the left
FULL OUTER JOIN – Returns all rows from both tables
CROSS JOIN – Returns Cartesian product of both tables
Q5: What is a primary key vs foreign key?
Answer:
-
Primary Key – Uniquely identifies each row in a table; cannot be
NULL. -
Foreign Key – A column referencing the primary key in another table, enforcing referential integrity.
2. Intermediate SQL Questions
Q6: What are aggregate functions?
-
COUNT(),SUM(),AVG(),MIN(),MAX() -
Often used with
GROUP BYandHAVING.
SELECT department_id, AVG(salary) AS avg_salaryFROM employeesGROUP BY department_idHAVING AVG(salary) > 60000;Q7: What are subqueries? Give an example.
Answer:A subquery is a query nested inside another query.Example:
SELECT first_name, last_nameFROM employeesWHERE salary > (SELECT AVG(salary)FROM employees);Returns employees earning more than the average salary.Q8: What is a CTE (Common Table Expression)?
Answer:CTE is a temporary named result set defined usingWITHto improve readability.WITH TopEmployees AS (SELECT first_name, salaryFROM employeesORDER BY salary DESCLIMIT 5)SELECT * FROM TopEmployees;Q9: What are indexes?
Answer:An index improves query speed by allowing the database to search quickly instead of scanning the entire table.Example:
CREATE INDEX idx_employee_lastnameON employees (last_name);
Indexes are useful for
WHERE,JOIN, andORDER BY.Q10: What is the difference between
UNIONandUNION ALL?Answer:
UNION – Combines results from two queries and removes duplicates.
UNION ALL – Combines results including duplicates.
SELECT first_name FROM employeesUNIONSELECT first_name FROM managers;3. Advanced SQL Questions
Q11: Explain window functions. Give an example.
Answer:Window functions perform calculations over a set of rows related to the current row,without collapsing results like
GROUP BY.SELECT first_name, salary,RANK() OVER (ORDER BY salary DESC) AS salary_rankFROM employees;
Assigns ranks based on salary.
Q12: How do you optimize SQL queries?
Answer:
-
Use indexes on frequently searched columns.
-
Select only necessary columns, avoid
SELECT *. -
Filter rows early with
WHERE. -
Avoid unnecessary joins or nested subqueries.
-
Use EXPLAIN or execution plans to identify slow queries.
Q13: Difference between DELETE, TRUNCATE, and DROP
| Command | Purpose | Notes |
|---|---|---|
| DELETE | Removes rows based on condition | Can use WHERE; slower for large tables |
| TRUNCATE | Removes all rows | Faster, cannot use WHERE, resets identity |
| DROP | Deletes entire table | Structure and data removed |
Q14: What is normalization?
-
1NF – Atomic values, no repeating groups
-
2NF – 1NF + all non-key attributes fully dependent on primary key
-
3NF – 2NF + no transitive dependency
Q15: How do you handle NULL values in SQL?
Answer:
-
Use
IS NULLorIS NOT NULLin filters. -
Use functions like
COALESCE()to replace NULL with a default value.
SELECT first_name, COALESCE(phone_number, 'N/A') AS phoneFROM employees;
Comments
Post a Comment