SQL Interview Questions for Freshers
Last updated 2026-11-19
Fresher SQL rounds stick to a predictable set: basic SELECT queries, filtering and sorting, aggregates with GROUP BY, a simple join, keys and constraints, and the difference between pairs of commands such as DELETE and TRUNCATE. This page gives short, correct answers to those, each tied to a small students table so you can see the real result of every query.
If you are new to SQL, read the questions in order and run each query yourself in MySQL or PostgreSQL. When you are comfortable, move on to SQL joins, SQL query practice and the full SQL interview questions. For a plan for the round itself see how to crack a SQL interview.
| Audience | Final-year students and first-job candidates |
|---|---|
| Sample | students table, 6 rows |
| Dialects | Works in MySQL and PostgreSQL |
Sample data
A students table with a NULL city (Divya) and a tie in marks (Bhavna and Eshan both have 74). The queries below use it.
CREATE TABLE students (roll INT PRIMARY KEY, name VARCHAR(15), branch VARCHAR(5), marks INT, city VARCHAR(10));
INSERT INTO students VALUES (1,'Aarav','CSE',82,'Pune'),(2,'Bhavna','ECE',74,'Delhi'),(3,'Chetan','CSE',91,'Pune'),
(4,'Divya','ME',66,NULL),(5,'Eshan','ECE',74,'Mumbai'),(6,'Fatima','CSE',58,'Delhi');What freshers are expected to know
- Write SELECT with WHERE, ORDER BY, LIMIT and DISTINCT without looking anything up.
- Use COUNT, SUM, AVG, MIN and MAX with GROUP BY and HAVING.
- Explain primary key, foreign key and the basic join types.
- Handle NULL correctly with IS NULL and COALESCE.
- Name the differences: DELETE vs TRUNCATE vs DROP, WHERE vs HAVING, UNION vs UNION ALL.
SQL Interview Questions for Freshers: questions with worked answers
1 What is SQL, and what is a relational database?
easySQL (Structured Query Language) is the standard language for defining, querying and changing data in a relational database management system such as MySQL, PostgreSQL, Oracle or SQL Server. A relational database stores data in tables made of rows and columns, and relates tables through keys. SQL is declarative: you say what result you want, and the database decides how to compute it.
2 Write a query to display all students from CSE ordered by marks, highest first.
easyFilter with WHERE and sort with ORDER BY ... DESC. The default sort order is ascending. Without ORDER BY the row order is not guaranteed.
sqlSELECT name, marks FROM students WHERE branch = 'CSE' ORDER BY marks DESC; -- name | marks -- -------+------ -- Chetan | 91 -- Aarav | 82 -- Fatima | 583 How do you get unique values, and how do you limit the number of rows?
easyDISTINCT removes duplicate rows from the result. LIMIT n returns the first n rows in MySQL, PostgreSQL and SQLite, TOP n in SQL Server and FETCH FIRST n ROWS ONLY in Oracle 12c and later and PostgreSQL. Always pair LIMIT with ORDER BY, or you get an arbitrary subset.
sqlSELECT DISTINCT branch FROM students ORDER BY branch; -- branch -- ------ -- CSE -- ECE -- MEsqlSELECT name, marks FROM students ORDER BY marks DESC LIMIT 3; -- name | marks -- -------+------ -- Chetan | 91 -- Aarav | 82 -- Bhavna | 744 What does LIKE do? Which names start with A or end with a?
easy% matches any number of characters and _ matches exactly one. LIKE 'A%' matches names starting with A, '%a' names ending in a. Case sensitivity depends on the database: MySQL string comparisons are case-insensitive with default collations, while PostgreSQL LIKE is case-sensitive and ILIKE ignores case.
sqlSELECT name FROM students WHERE name LIKE 'A%' OR name LIKE '%a' ORDER BY roll; -- name -- ------ -- Aarav -- Bhavna -- Divya -- Fatima5 Find students with marks between 70 and 85, and students from Pune or Delhi.
easyBETWEEN is inclusive of both ends. IN checks membership in a list and is shorter than chained ORs.
sqlSELECT name, marks FROM students WHERE marks BETWEEN 70 AND 85 ORDER BY roll; -- name | marks -- -------+------ -- Aarav | 82 -- Bhavna | 74 -- Eshan | 74sqlSELECT name, city FROM students WHERE city IN ('Pune', 'Delhi') ORDER BY roll; -- name | city -- -------+------ -- Aarav | Pune -- Bhavna | Delhi -- Chetan | Pune -- Fatima | Delhi6 Why does "city = NULL" return nothing? How do you find students with no city?
mediumNULL means unknown, so comparing with = never gives true. Use IS NULL. COALESCE(city, 'Unknown') substitutes a default. Also, a condition like city <> 'Pune' does not return Divya, because NULL <> 'Pune' is unknown, so add OR city IS NULL if you want her.
sqlSELECT name, COALESCE(city, 'Unknown') AS city FROM students WHERE city IS NULL; -- name | city -- ------+-------- -- Divya | Unknown7 Find the average, highest and lowest marks, and the number of students.
easyAggregate functions summarise many rows into one. AVG ignores NULLs. Without GROUP BY the whole table is one group.
sqlSELECT COUNT(*) AS students, ROUND(AVG(marks), 2) AS avg_marks, MAX(marks) AS top, MIN(marks) AS lowest FROM students; -- students | avg_marks | top | lowest -- ---------+-----------+-----+------- -- 6 | 74.17 | 91 | 588 Show the number of students and average marks per branch, only for branches with more than one student.
mediumGROUP BY forms the groups, and HAVING filters the groups. ME has one student and is excluded. WHERE could not do this, because it runs before grouping.
sqlSELECT branch, COUNT(*) AS students, ROUND(AVG(marks), 1) AS avg_marks FROM students GROUP BY branch HAVING COUNT(*) > 1 ORDER BY branch; -- branch | students | avg_marks -- -------+----------+---------- -- CSE | 3 | 77 -- ECE | 2 | 749 Find the second highest marks.
mediumTake the maximum of the values below the maximum. This works on every database. The answer is 82, and the tie at 74 does not matter because values, not rows, are compared. More variants in Nth highest salary questions.
sqlSELECT MAX(marks) AS second_highest FROM students WHERE marks < (SELECT MAX(marks) FROM students); -- second_highest -- -------------- -- 8210 What is the difference between primary key and foreign key?
easyA primary key uniquely identifies each row in its table and cannot be NULL; there is one per table, though it may span several columns. A foreign key is a column that refers to the primary key of another table, enforcing that the value exists there (referential integrity). For example, enrolments.roll referencing students.roll prevents an enrolment for a student that does not exist.
11 What are the types of joins? Write an INNER JOIN.
easyINNER JOIN returns matching rows only. LEFT JOIN returns all left rows plus matches. RIGHT JOIN is the mirror image, FULL JOIN returns all rows from both sides and CROSS JOIN gives every pairing. The tables below add a branch lookup; the join shows each student with the full branch name. Full details in SQL joins questions.
sqlSELECT s.name, b.full_name FROM students s INNER JOIN branches b ON b.code = s.branch ORDER BY s.roll; -- name | full_name -- -------+----------------- -- Aarav | Computer Science -- Bhavna | Electronics -- Chetan | Computer Science -- Divya | Mechanical -- Eshan | Electronics -- Fatima | Computer Science12 What is the difference between DELETE, TRUNCATE and DROP?
easyDELETE removes selected rows (WHERE allowed), can be rolled back and fires triggers. TRUNCATE empties the whole table quickly and keeps its structure; it has no WHERE and in MySQL cannot be rolled back. DROP deletes the table definition and its data. Use DELETE for some rows, TRUNCATE to reset a table, DROP to remove it.
13 What is the difference between WHERE and HAVING, and between COUNT(*) and COUNT(column)?
mediumWHERE filters rows before grouping; HAVING filters groups after aggregates are computed. COUNT(*) counts all rows, COUNT(city) counts only rows where city is not NULL. On this table the first is 6 and the second is 5, because Divya has no city.
sqlSELECT COUNT(*) AS all_rows, COUNT(city) AS with_city FROM students; -- all_rows | with_city -- ---------+---------- -- 6 | 514 How do you update and delete safely?
mediumWrite the WHERE clause first and test it with a SELECT, because UPDATE or DELETE without WHERE changes every row. Wrap risky changes in a transaction so you can ROLLBACK. MySQL Workbench has safe-update mode that refuses UPDATE or DELETE without a key in WHERE. Many production databases also take a backup or snapshot first.
sqlSTART TRANSACTION; UPDATE students SET marks = marks + 5 WHERE roll = 6; SELECT roll, marks FROM students WHERE roll = 6; -- check: 63 COMMIT; -- or ROLLBACK15 What is a subquery? Show students scoring above the average.
mediumA query nested inside another. The inner query runs first and returns the average, 74.1666, and the outer query keeps students above it. See SQL subqueries.
sqlSELECT name, marks FROM students WHERE marks > (SELECT AVG(marks) FROM students) ORDER BY marks DESC; -- name | marks -- -------+------ -- Chetan | 91 -- Aarav | 82
Frequently asked questions
Is SQL difficult for freshers?
The basics are quick to learn. Joins and GROUP BY need practice on small tables, and window functions come later.
Which SQL database should a fresher learn first?
MySQL or PostgreSQL. Both are free and the syntax you learn carries across, with small differences.
Do freshers get window function questions?
Sometimes, for analyst and product-company roles. Start with window function questions after the basics.
How many queries should I practise daily?
Writing five to ten small queries by hand each day for a couple of weeks usually builds fluency. Quality matters more than count.
Related guides
More in Programming Language Interview Questions by Language
- SQL Joins Interview Questions
- SQL Query Practice: Interview Questions with Solutions
- SQL Subqueries Interview Questions
- SQL Window Functions Interview Questions
- TypeScript Interview Questions and Answers
- .NET Core and .NET 8 Interview Questions
- Advanced Python Interview Questions
- ASP.NET Core MVC Interview Questions
- C Language Interview Questions and Answers
- C Pointers Interview Questions
- C Programming Output Questions with Answers
- C# Async Await Interview Questions