Skip to content
Laviyx Learn

Try

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.

Beginner6 min readUpdated
On this page

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:

idnamedepartmentsalarycitymanager_id
1AshaSales52000Mumbai5
2RaviEngineering78000Bengaluru3
3MeeraEngineering81000PuneNULL
4JohnMarketing46000Mumbai5
5PriyaSales61000DelhiNULL

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
CategoryPurposeCommands
DQL (query)read dataSELECT
DML (manipulation)change dataINSERT, UPDATE, DELETE
DDL (definition)change structureCREATE, ALTER, DROP, TRUNCATE
DCL (control)permissionsGRANT, REVOKE
TCL (transaction control)transactionsCOMMIT, ROLLBACK, SAVEPOINT

Some sources fold SELECT into DML; mention that both conventions exist.

4. What is the difference between DELETE, TRUNCATE and DROP?#

Show solution
  • DELETE removes selected rows (with WHERE), one by one, and can be rolled back.
  • TRUNCATE removes all rows quickly and usually resets auto-increment counters; it cannot have a WHERE.
  • DROP removes 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
SQLRun
SELECT name, salary
FROM employees
WHERE city = 'Mumbai'
ORDER BY salary DESC;
Output
name | salary
-----+-------
Asha | 52000
John | 46000

10. Find the second-highest salary.#

Show hint

The second-highest salary is the highest salary that is lower than the maximum.

Show solution
SQLRun
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
Output
second_highest
--------------
78000

This 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
SQLRun
SELECT department, COUNT(*) AS employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 1;
Output
department  | employees
------------+----------
Engineering | 2
Sales       | 2

12. Find employees whose name starts with "A" or ends with "a".#

Show solution
SQLRun
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:

SQLRun
SELECT city, COUNT(*) AS n
FROM employees
GROUP BY city
HAVING COUNT(*) > 1;
Output
city   | n
-------+--
Mumbai | 2

The 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?