30 SQL Interview Questions With Real-Time Answers

30 SQL Interview Questions With Real-Time Answers

Learning SQL becomes easier when you practice with real, everyday business scenarios. In this guide, you’ll find 30 SQL queries explained with real-time use cases, beginner-friendly breakdowns, and clean examples.

Let’s begin by imagining two common tables used in most companies:

Create Database

CREATE DATABASE simha_it_practice;
USE simha_it_practice;

📊 Sample Tables

1. Employees Table

CREATE TABLE employees (
    employee_id INT PRIMARY KEY,
    name VARCHAR(50),
    department VARCHAR(50),
    salary INT,
    join_date DATE
);

Insert Sample Data

INSERT INTO employees (employee_id, name, department, salary, join_date) VALUES
(1, 'Rohan', 'IT', 55000, '2021-04-15'),
(2, 'Sneha', 'HR', 45000, '2020-06-10'),
(3, 'Arjun', 'Finance', 60000, '2022-01-20'),
(4, 'Kavya', 'IT', 75000, '2021-10-10'),
(5, 'Manoj', 'Marketing', 40000, '2019-12-01');
employee_idnamedepartmentsalaryjoin_date
1RohanIT550002021-04-15
2SnehaHR450002020-06-10
3ArjunFinance600002022-01-20
4KavyaIT750002021-10-10
5ManojMarketing400002019-12-01

2. Orders Table

CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    customer_name VARCHAR(50),
    amount INT,
    order_date DATE,
    status VARCHAR(20)
);

Insert Sample Data

INSERT INTO orders (order_id, customer_name, amount, order_date, status) VALUES
(101, 'Rahul', 1200, '2023-03-01', 'Delivered'),
(102, 'Neha', 800, '2023-03-05', 'Pending'),
(103, 'Simran', 600, '2023-03-05', 'Delivered'),
(104, 'Rajesh', 3500, '2023-03-06', 'Cancelled'),
(105, 'Vivek', 1500, '2023-03-07', 'Delivered');
order_idcustomer_nameamountorder_datestatus
101Rahul12002023-03-01Delivered
102Neha8002023-03-05Pending
103Simran6002023-03-05Delivered
104Rajesh35002023-03-06Cancelled
105Vivek15002023-03-07Delivered

SQL Interview Questions and Answers

Write a query to fetch all records from the Employees table.

SELECT * FROM employees;

How do you fetch only name and department from Employees?

SELECT name, department FROM employees;

Get all employees from the IT department.

SELECT * FROM employees WHERE department = 'IT';

Fetch employees earning more than 50,000.

SELECT name, salary FROM employees WHERE salary > 50000;

Sort employees by highest salary.

SELECT * FROM employees ORDER BY salary DESC;

Count the total number of employees.

SELECT COUNT(*) FROM employees;

Find the minimum salary.

SELECT MIN(salary) FROM employees;

Find the maximum salary.

SELECT MAX(salary) FROM employees;

Find the average salary.

SELECT AVG(salary) FROM employees;

Get number of employees in each department.

SELECT department, COUNT(*) FROM employees GROUP BY department;

Get departments where average salary is above 50,000.

SELECT department, AVG(salary) 
FROM employees 
GROUP BY department 
HAVING AVG(salary) > 50000;

List employees who joined after Jan 2021.

SELECT * FROM employees WHERE join_date > '2021-01-01';

Get employees whose names start with ‘S’.

SELECT * FROM employees WHERE name LIKE 'S%';

Fetch orders with amount greater than 1000.

SELECT * FROM orders WHERE amount > 1000;

Get all pending orders.

SELECT * FROM orders WHERE status = 'Pending';

Count delivered orders.

SELECT COUNT(*) FROM orders WHERE status = 'Delivered';

Get most recent orders.

SELECT * FROM orders ORDER BY order_date DESC;

Find total sales amount.

SELECT SUM(amount) FROM orders;

Get monthly sales summary.

SELECT MONTH(order_date), SUM(amount) 
FROM orders 
GROUP BY MONTH(order_date);

Fetch orders from a specific date (2023-03-05).

SELECT * FROM orders WHERE order_date = '2023-03-05';

Get top 3 highest salaries.

SELECT * 
FROM employees 
ORDER BY salary DESC 
LIMIT 3;

Fetch unique departments.

SELECT DISTINCT department FROM employees;

Get employees with salary between 40,000 and 60,000.

SELECT * FROM employees WHERE salary BETWEEN 40000 AND 60000;

Get orders where customer name starts with ‘R’.

SELECT * FROM orders WHERE customer_name LIKE 'R%';

Write a JOIN example.

SELECT e.name, o.order_id, o.amount
FROM employees e
JOIN orders o ON e.employee_id = o.order_id;

Get departments having more than 1 employee.

SELECT department, COUNT(*) 
FROM employees 
GROUP BY department 
HAVING COUNT(*) > 1;

Delete employee with ID = 5.

DELETE FROM employees WHERE employee_id = 5;

Update salary of employee with ID = 1.

UPDATE employees SET salary = 65000 WHERE employee_id = 1;

Insert a new employee record.

INSERT INTO employees (employee_id, name, department, salary, join_date)
VALUES (6, 'Tejas', 'IT', 50000, '2023-02-01');

Find highest salary in each department.

SELECT department, MAX(salary) 
FROM employees 
GROUP BY department;

FAQs: 30 SQL Interview Questions With Real-Time Answers & Queries

What should I learn first before practicing SQL queries?

Start with table structure basics — columns, rows, data types, and primary keys. Once you understand how data is stored, writing SELECT, WHERE, and JOIN queries becomes easier and more meaningful.

How do I know if my SQL query is written efficiently?

Check if the query avoids unnecessary subqueries, uses proper filtering, and leverages indexes on key columns. If the same task can be done with fewer steps or simpler joins, it’s likely more efficient.

Why do interviewers ask scenario-based SQL questions instead of simple queries?

Because real work rarely involves textbook queries. Scenario questions show how you think through messy datasets, unclear requirements, missing values, and performance constraints — all of which exist in real projects.

How can I practice SQL without installing any software?

Use online editors such as SQL Fiddle, DB Fiddle, or interactive learning platforms. They let you run queries on sample databases instantly, so you can practice anywhere with just a browser.

What makes a SQL query “real-time” in practical projects?

Real-time queries usually involve joining multiple tables, filtering large datasets, handling NULL values, grouping results, and generating insights—like sales summaries, user activity logs, or product performance reports.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top