Autocoder

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.

AudienceFinal-year students and first-job candidates
Samplestudents table, 6 rows
DialectsWorks 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.

sql
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

SQL Interview Questions for Freshers: questions with worked answers

  1. 1

    What is SQL, and what is a relational database?

    easy

    SQL (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. 2

    Write a query to display all students from CSE ordered by marks, highest first.

    easy

    Filter with WHERE and sort with ORDER BY ... DESC. The default sort order is ascending. Without ORDER BY the row order is not guaranteed.

    sql
    SELECT name, marks FROM students WHERE branch = 'CSE' ORDER BY marks DESC;
    -- name   | marks
    -- -------+------
    -- Chetan | 91
    -- Aarav  | 82
    -- Fatima | 58
  3. 3

    How do you get unique values, and how do you limit the number of rows?

    easy

    DISTINCT 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.

    sql
    SELECT DISTINCT branch FROM students ORDER BY branch;
    -- branch
    -- ------
    -- CSE
    -- ECE
    -- ME
    sql
    SELECT name, marks FROM students ORDER BY marks DESC LIMIT 3;
    -- name   | marks
    -- -------+------
    -- Chetan | 91
    -- Aarav  | 82
    -- Bhavna | 74
  4. 4

    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.

    sql
    SELECT name FROM students WHERE name LIKE 'A%' OR name LIKE '%a' ORDER BY roll;
    -- name
    -- ------
    -- Aarav
    -- Bhavna
    -- Divya
    -- Fatima
  5. 5

    Find students with marks between 70 and 85, and students from Pune or Delhi.

    easy

    BETWEEN is inclusive of both ends. IN checks membership in a list and is shorter than chained ORs.

    sql
    SELECT name, marks FROM students WHERE marks BETWEEN 70 AND 85 ORDER BY roll;
    -- name   | marks
    -- -------+------
    -- Aarav  | 82
    -- Bhavna | 74
    -- Eshan  | 74
    sql
    SELECT name, city FROM students WHERE city IN ('Pune', 'Delhi') ORDER BY roll;
    -- name   | city
    -- -------+------
    -- Aarav  | Pune
    -- Bhavna | Delhi
    -- Chetan | Pune
    -- Fatima | Delhi
  6. 6

    Why does "city = NULL" return nothing? How do you find students with no city?

    medium

    NULL 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.

    sql
    SELECT name, COALESCE(city, 'Unknown') AS city FROM students WHERE city IS NULL;
    -- name  | city
    -- ------+--------
    -- Divya | Unknown
  7. 7

    Find the average, highest and lowest marks, and the number of students.

    easy

    Aggregate functions summarise many rows into one. AVG ignores NULLs. Without GROUP BY the whole table is one group.

    sql
    SELECT 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  | 58
  8. 8

    Show the number of students and average marks per branch, only for branches with more than one student.

    medium

    GROUP 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.

    sql
    SELECT 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        | 74
  9. 9

    Find the second highest marks.

    medium

    Take 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.

    sql
    SELECT MAX(marks) AS second_highest FROM students WHERE marks < (SELECT MAX(marks) FROM students);
    -- second_highest
    -- --------------
    -- 82
  10. 10

    What is the difference between primary key and foreign key?

    easy

    A 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. 11

    What are the types of joins? Write an INNER JOIN.

    easy

    INNER 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.

    sql
    SELECT 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 Science
  12. 12

    What is the difference between DELETE, TRUNCATE and DROP?

    easy

    DELETE 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. 13

    What is the difference between WHERE and HAVING, and between COUNT(*) and COUNT(column)?

    medium

    WHERE 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.

    sql
    SELECT COUNT(*) AS all_rows, COUNT(city) AS with_city FROM students;
    -- all_rows | with_city
    -- ---------+----------
    -- 6        | 5
  14. 14

    How do you update and delete safely?

    medium

    Write 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.

    sql
    START TRANSACTION;
    UPDATE students SET marks = marks + 5 WHERE roll = 6;
    SELECT roll, marks FROM students WHERE roll = 6;   -- check: 63
    COMMIT;   -- or ROLLBACK
  15. 15

    What is a subquery? Show students scoring above the average.

    medium

    A 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.

    sql
    SELECT 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

See all in this section