SQL Interview Questions for Freshers
Basic SQL Questions
What is SQL? Explain its uses.
What are the different types of SQL commands? (e.g., SELECT, INSERT, UPDATE, DELETE, DDL,
DML, DCL, TCL).
What is the difference between WHERE and HAVING?
What are Primary Key and Foreign Key?
Explain the difference between DELETE and TRUNCATE.
What are the different types of joins in SQL?
What is the use of the GROUP BY clause?
What are indexes in SQL? Why are they used?
What is a unique constraint? How does it differ from a primary key?
What is a view, and why would you use it?
Query-Based Questions
Write a query to fetch all records from the Employees table where the salary is greater than 5000:
SELECT * FROM Employees WHERE salary > 5000;
Write a query to find the second-highest salary from the Salaries table:
SELECT MAX(salary) FROM Salaries WHERE salary < (SELECT MAX(salary) FROM Salaries);
Write a query to count the number of employees in each department:
SELECT department_id, COUNT(*) FROM Employees GROUP BY department_id;
Fetch the details of employees who joined in the year 2023:
SELECT * FROM Employees WHERE YEAR(join_date) = 2023;
Write a query to find duplicate records in a table:
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING
COUNT(*) > 1;
Fetch the top 3 highest salaries from the Salaries table:
SELECT salary FROM Salaries ORDER BY salary DESC LIMIT 3;
Scenario-Based Questions
How would you delete duplicate rows in a table while keeping one copy:
DELETE FROM table_name WHERE id NOT IN (SELECT MIN(id) FROM table_name GROUP BY
duplicate_column);
If a table has a million records, how would you optimize a query for better performance?
Explain how you would handle NULL values in a database query.
Write a query to fetch all employees whose names start with 'A' and end with 'n':
SELECT * FROM Employees WHERE name LIKE 'A%n';
Write a query to find all employees whose salary is between 10,000 and 20,000:
SELECT * FROM Employees WHERE salary BETWEEN 10000 AND 20000;
Advanced Beginner Questions
What is a subquery, and how is it different from a join?
What is the difference between INNER JOIN and OUTER JOIN?
Explain the use of COALESCE and CASE in SQL.
What is the difference between RANK(), DENSE_RANK(), and ROW_NUMBER() in SQL?
What are Common Table Expressions (CTEs), and how are they used?
Tips for Freshers
Practice writing queries in a real database environment (e.g., MySQL, PostgreSQL, SQL Server).
Focus on understanding database concepts, not just syntax.
Be prepared to explain the logic behind your queries.