SQL Interview Questions for Freshers with Answers
Most entry-level SQL interviews stick to a predictable core: basic querying, JOINs, keys, normalization, and the differences between commands that sound similar. This guide walks through the questions freshers get asked most often, grouped by topic, with clear answers and runnable examples so you can practice out loud before the real thing.
1. SELECT Basics
Q: How do you retrieve specific columns from a table with a condition?
Use SELECT with a column list and a WHERE clause. Avoid SELECT * in real applications — it pulls more data than needed and breaks if columns are added later.
SELECT first_name, last_name, salary FROM employees WHERE department = 'Engineering' AND salary > 50000 ORDER BY salary DESC;
Q: What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping happens. HAVING filters groups after GROUP BY has aggregated them. You cannot use an aggregate function like COUNT() or SUM() inside WHERE, but you can inside HAVING.
SELECT department, COUNT(*) AS employee_count FROM employees WHERE status = 'active' GROUP BY department HAVING COUNT(*) > 5;
2. JOIN Types
Q: What are the main types of JOINs and how do they differ?
- INNER JOIN — returns only rows that match in both tables
- LEFT JOIN — returns all rows from the left table, with NULLs where there is no match in the right table
- RIGHT JOIN — returns all rows from the right table, with NULLs where there is no match in the left table
- FULL OUTER JOIN — returns all rows from both tables, with NULLs where there is no match on either side (not directly supported in MySQL; simulate with UNION of LEFT and RIGHT JOIN)
-- Customers who have never placed an order SELECT c.customer_id, c.name FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id WHERE o.order_id IS NULL;
3. Primary Keys and Foreign Keys
Q: What is the difference between a primary key and a foreign key?
A primary key uniquely identifies each row in its own table — it cannot be NULL and cannot repeat. A foreign key is a column in one table that references the primary key of another table, enforcing a relationship between the two and preventing orphaned rows.
CREATE TABLE customers ( customer_id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100) NOT NULL ); CREATE TABLE orders ( order_id INT PRIMARY KEY AUTO_INCREMENT, customer_id INT, order_total DECIMAL(10,2), FOREIGN KEY (customer_id) REFERENCES customers(customer_id) );
Q: Can a table have more than one foreign key?
Yes. A table can have as many foreign keys as it needs — an orders table might reference both a customers table and a shipping_addresses table. It can only have one primary key, though that key can span multiple columns (a composite key).
4. Normalization Basics
Q: What are 1NF, 2NF, and 3NF in simple terms?
- 1NF (First Normal Form) — every column holds a single, atomic value; no repeating groups or comma-separated lists in one cell
- 2NF (Second Normal Form) — the table is in 1NF, and every non-key column depends on the whole primary key, not just part of a composite key
- 3NF (Third Normal Form) — the table is in 2NF, and no non-key column depends on another non-key column (no transitive dependency)
Example: storing customer_city and customer_zip directly on an orders table violates 3NF, because those columns depend on the customer, not the order. They belong in the customers table instead.
5. DELETE vs TRUNCATE vs DROP
Q: What is the difference between DELETE, TRUNCATE, and DROP?
- DELETE — removes rows one at a time, supports a WHERE clause, is fully logged (row by row), and can be rolled back inside a transaction
- TRUNCATE — removes all rows instantly, cannot use WHERE, resets AUTO_INCREMENT, is minimally logged, and is much faster than DELETE for clearing a whole table
- DROP — deletes the table itself, including its structure, indexes, and constraints, not just the data
DELETE FROM orders WHERE order_status = 'cancelled'; -- selective, rollback-able TRUNCATE TABLE staging_orders; -- clears entire table fast DROP TABLE old_orders_backup; -- removes the table entirely
Quick Reference: More Common Fresher Questions
- UNIQUE vs PRIMARY KEY — a table can have multiple UNIQUE constraints but only one PRIMARY KEY; UNIQUE columns can allow one NULL, PRIMARY KEY cannot allow any NULL
- WHERE vs HAVING with GROUP BY — WHERE filters before grouping, HAVING filters after
- DISTINCT — removes duplicate rows from a result set, applied across all selected columns together
- NULL comparisons — use
IS NULL/IS NOT NULL, never= NULL, since NULL is not equal to anything, including itself - Aggregate functions — COUNT, SUM, AVG, MIN, MAX all ignore NULL values except COUNT(*), which counts every row
Frequently Asked Questions
Freshers should be comfortable with SELECT queries and filtering, all JOIN types (INNER, LEFT, RIGHT, FULL), primary and foreign keys, basic normalization (1NF, 2NF, 3NF), aggregate functions with GROUP BY, and the differences between DELETE, TRUNCATE, and DROP.
DELETE removes rows one at a time, can use a WHERE clause, is logged, and can be rolled back. TRUNCATE removes all rows at once, resets auto-increment counters, is minimally logged, and is faster than DELETE. DROP removes the entire table structure along with its data, indexes, and constraints permanently.
Yes. Dev Brains AI free AI SQL Query Builder lets you type a question in plain English and see the matching SQL query, which is a fast way to check your own answers while practicing for interviews.