SQL Interview Questions and Answers
Basic to Intermediate (1–20)
1. Select all records from a table.
SELECT * FROM table_name;
2. Select only name and salary columns from an employee table.
SELECT name, salary FROM employee;
3. Filter orders placed after January 1, 2023.
SELECT * FROM orders WHERE order_date > '2023-01-01';
4. Sort products by price (highest to lowest).
SELECT * FROM products ORDER BY price DESC;
5. Retrieve the first 10 customers.
SELECT * FROM customers LIMIT 10;
6. Get unique departments from an employee table.
SELECT DISTINCT department FROM employee;
7. Find employees with a salary > $50,000 in the Sales department.
SELECT * FROM employee WHERE salary > 50000 AND department = 'Sales';
8. Retrieve orders with amounts between 100 and 1000.
SELECT * FROM orders WHERE amount BETWEEN 100 AND 1000;
9. Find customers whose names start with "John".
SELECT * FROM customers WHERE name LIKE 'John%';
10. Get products belonging to categories 1, 5, or 7.
SELECT * FROM products WHERE category_id IN (1, 5, 7);
11. Find employees without a manager (NULL check).
SELECT * FROM employee WHERE manager_id IS NULL;
12. Categorize salaries as "High" (>100K) or "Low" (≤100K).
SELECT name, salary,
CASE WHEN salary > 100000 THEN 'High'
ELSE 'Low'
END AS salary_category
FROM employee;
13. Concatenate first and last names into a full name.
SELECT first_name || ' ' || last_name AS full_name FROM employee;
14. Extract the year from an order date.
SELECT EXTRACT(YEAR FROM order_date) AS order_year FROM orders;
15. Replace NULL bonuses with 0.
SELECT COALESCE(bonus, 0) AS bonus FROM employee;
16. Convert salary from integer to string.
SELECT CAST(salary AS VARCHAR) AS salary_str FROM employee;
17. Round average salary to 2 decimal places.
SELECT ROUND(AVG(salary), 2) AS avg_salary FROM employee;
18. Extract the first 5 characters of customer emails.
SELECT SUBSTRING(email FROM 1 FOR 5) AS short_email FROM customers;
19. Add 7 days to an order date to get a due date.
SELECT order_date, order_date + INTERVAL '7 days' AS due_date FROM orders;
20. Use a CTE to find high-earning employees (>100K).
WITH high_earners AS (
SELECT * FROM employee
WHERE salary > 100000
)
SELECT * FROM high_earners;