SQL course menu
Beginner SQL Interview Questions and Answers
The SQL interview questions beginners are asked most: what SQL is, keys, WHERE vs HAVING, DELETE vs TRUNCATE vs DROP, NULL handling, DISTINCT, ORDER BY and simple queries, with clear answers.
On this page
- Concepts
- 1. What is SQL, and what is a relational database?
- 2. What is the difference between a primary key and a foreign key?
- 3. What are the main categories of SQL commands?
- 4. What is the difference between DELETE, TRUNCATE and DROP?
- 5. What is NULL? How do you check for it?
- 6. What is the difference between WHERE and HAVING?
- 7. What does DISTINCT do?
- 8. What is the difference between CHAR and VARCHAR?
- Queries
- 9. Return the employees in Mumbai, highest salary first.
- 10. Find the second-highest salary.
- 11. Count employees per department, showing only departments with more than one employee.
- 12. Find employees whose name starts with "A" or ends with "a".
- 13. Find duplicate values in a column.
- Tips for beginner interviews
These are the questions that come up in almost every entry-level SQL interview: for analyst, data, QA, and junior developer roles. Try to answer each one out loud before opening the answer. Interviewers care about clear explanations as much as correct syntax.
All query examples use the course's sample employees table:
Concepts#
1. What is SQL, and what is a relational database?#
Show solution
SQL (Structured Query Language) is the standard language for reading and changing data in relational databases. A relational database stores data in tables of rows and columns, and connects tables through keys. MySQL, PostgreSQL, SQL Server, Oracle and SQLite are relational database systems that all speak SQL. See What is SQL?.
2. What is the difference between a primary key and a foreign key?#
Show solution
A primary key uniquely identifies each row in its own table; it must be unique and not NULL. A foreign key is a column that refers to the primary key of another (or the same) table, linking related rows and preventing references to rows that do not exist. In our table, id is the primary key and manager_id is a foreign key pointing back to id.
3. What are the main categories of SQL commands?#
Show hint
Think of reading data, changing data, changing structure, and permissions.
Show solution
Some sources fold SELECT into DML; mention that both conventions exist.
4. What is the difference between DELETE, TRUNCATE and DROP?#
Show solution
DELETEremoves selected rows (withWHERE), one by one, and can be rolled back.TRUNCATEremoves all rows quickly and usually resets auto-increment counters; it cannot have aWHERE.DROPremoves the whole table: data and structure.
See DELETE and DROP TABLE.
5. What is NULL? How do you check for it?#
Show solution
NULL means a value is missing or unknown. It is not zero and not an empty string. Any comparison with NULL using = or <> gives "unknown", so WHERE manager_id = NULL returns nothing. Use IS NULL / IS NOT NULL, and COALESCE(column, default) to replace missing values. See NULL Values.
6. What is the difference between WHERE and HAVING?#
Show solution
WHERE filters rows before grouping and cannot use aggregate functions. HAVING filters groups after GROUP BY and is where conditions like COUNT(*) > 1 go. See HAVING.
7. What does DISTINCT do?#
Show solution
SELECT DISTINCT removes duplicate rows from the result. With several columns, it removes rows where the whole combination is repeated. COUNT(DISTINCT column) counts unique non-NULL values. See DISTINCT.
8. What is the difference between CHAR and VARCHAR?#
Show solution
CHAR(n) always stores exactly n characters, padding shorter values with spaces; it suits fixed-length codes. VARCHAR(n) stores up to n characters with no padding; it is the normal choice for names, emails and other variable text. See Data Types.
Queries#
9. Return the employees in Mumbai, highest salary first.#
Show solution
SELECT name, salary
FROM employees
WHERE city = 'Mumbai'
ORDER BY salary DESC;name | salary
-----+-------
Asha | 52000
John | 4600010. Find the second-highest salary.#
Show hint
The second-highest salary is the highest salary that is lower than the maximum.
Show solution
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);second_highest
--------------
78000This handles a tie at the top correctly. Mention the alternative ORDER BY salary DESC LIMIT 1 OFFSET 1, and that it gives the wrong answer if two people share the top salary. DENSE_RANK generalises it to the Nth highest.
11. Count employees per department, showing only departments with more than one employee.#
Show solution
SELECT department, COUNT(*) AS employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 1;department | employees
------------+----------
Engineering | 2
Sales | 212. Find employees whose name starts with "A" or ends with "a".#
Show solution
SELECT name
FROM employees
WHERE name LIKE 'A%' OR name LIKE '%a';Asha matches both patterns, Meera and Priya match the second. % matches any number of characters; _ matches exactly one.
13. Find duplicate values in a column.#
Show hint
Group by the column and keep groups with more than one row.
Show solution
Cities shared by more than one employee:
SELECT city, COUNT(*) AS n
FROM employees
GROUP BY city
HAVING COUNT(*) > 1;city | n
-------+--
Mumbai | 2The same pattern finds duplicate emails, phone numbers or order references.
Tips for beginner interviews#
- Say what you are doing as you write: "first I'll filter, then group, then sort".
- Know the logical order of a query:
FROM→WHERE→GROUP BY→HAVING→SELECT→ORDER BY→LIMIT. It explains many "why doesn't this work?" questions. - Mention edge cases without being asked: ties,
NULLs, empty results. - If you do not remember exact syntax for a specific database, say so and describe the idea; that is far better than guessing.
Ready for more? Continue with Intermediate SQL Questions.
Finished this lesson?