SQL interview Questions and Answers

 SQL interview Questions & Answers 




 Complete Roadmap to Learn SQL (Structured Query Language) 


Week 1: SQL Basics

- What is SQL and how databases work

- Install MySQL Workbench or PostgreSQL

- Learn SELECT, FROM, WHERE

- Filtering data with conditions

- Practice basic queries

Example: Fetch all employees, filter salary > 50k


Week 2: Sorting and Aggregation

- ORDER BY (sorting data)

- Aggregate functions: COUNT, SUM, AVG, MIN, MAX

- GROUP BY concept

- HAVING clause

Example: Department-wise average salary


Week 3: Joins (Most Important )

- INNER JOIN

- LEFT JOIN, RIGHT JOIN

- FULL JOIN

- Self Join

Example: Combine employees and departments tables


Week 4: Advanced Filtering

- IN, BETWEEN, LIKE

- Wildcards (% , _)

- NULL handling (IS NULL, IS NOT NULL)

- CASE statements

Example: Categorize customers based on spending


Week 5: Subqueries

- Nested queries

- Correlated subqueries

- Using subqueries in SELECT, WHERE

Example: Find employees earning above average salary


Week 6: Window Functions (High Value )

- OVER() clause

- ROW_NUMBER(), RANK(), DENSE_RANK()

- PARTITION BY

Example: Rank employees by salary within each department


Week 7: CTE & Views

- Common Table Expressions (WITH)

- Temporary vs permanent views

- Simplify complex queries

Example: Multi-step data transformation


Week 8: Data Modification

- INSERT, UPDATE, DELETE

- TRUNCATE vs DELETE

- Constraints (PRIMARY KEY, FOREIGN KEY)

Example: Update employee salary


Week 9: Indexing & Performance

- What are indexes

- Query optimization basics

- EXPLAIN keyword

Example: Speed up large table queries


Week 10: Working with Real Data

- Import CSV data

- Data cleaning in SQL

- Handling duplicates

- Basic transformations

Example: Clean messy sales dataset


Week 11: Mini Projects

- Write complex queries

- Solve real-world case studies

- Focus on business logic

Examples: Sales dashboard queries, Customer segmentation


Week 12: Final Preparation

- Revise all concepts

- Practice interview questions

- Solve SQL challenges on LeetCode / HackerRank

- Mock interviews


Daily Rule for You

- Practice SQL 60 minutes daily

- Solve 5 queries daily

- Revise previous queries weekly


Pro Tip

- Focus more on JOINS + WINDOW FUNCTIONS

- Practice real datasets, not just theory

- Think in terms of “business questions”



Core SQL Interview Questions With Answers


1 What is SQL

• SQL stands for Structured Query Language

• You use it to read and manage data in relational databases

• Used in MySQL, PostgreSQL, SQL Server, Oracle


2 What is an RDBMS

• Relational Database Management System

• Stores data in tables with rows and columns

• Uses keys to link tables

• Example. Customer table linked to Orders table using customer_id


3 What is a table

• Structured storage for data

• Rows are records

• Columns are attributes

• Example. One row equals one customer


4 What is a primary key

• Uniquely identifies each row

• Cannot be NULL

• No duplicate values

• Example. user_id in users table


5 What is a foreign key

• Links one table to another

• Refers to a primary key in another table

• Allows duplicate values

• Example. user_id in orders table


6 Difference between primary key and foreign key

• Primary key ensures uniqueness

• Foreign key ensures relationship

• One table can have one primary key

• One table can have multiple foreign keys


7 What is NULL

• Represents missing or unknown value

• Not equal to zero or empty string

• Use IS NULL or IS NOT NULL to check


8 What are constraints

• Rules applied on columns

• Maintain data quality

• Common constraints

– NOT NULL

– UNIQUE

– PRIMARY KEY

– FOREIGN KEY

– CHECK


9 What are data types

• Define type of data stored

• Common types

– INT for numbers

– VARCHAR for text

– DATE for dates

– FLOAT or DECIMAL for decimals


10 Interview tip you must remember

• Always explain with a small example

• Speak logic before syntax

• Keep answers short and direct


11. What is SELECT

- Retrieves specific data from tables

- Choose columns with * for all

- Example: SELECT name, age FROM users;


12. What does WHERE do

- Filters rows based on conditions

- Applied after FROM clause

- Example: SELECT * FROM users WHERE age > 25 [1]


13. What is ORDER BY

- Sorts result set by column(s)

- ASC (default) or DESC

- Example: SELECT * FROM users ORDER BY age DESC;


14. What is GROUP BY

- Groups rows with same values

- Used with aggregate functions like COUNT, SUM

- Example: SELECT department, COUNT(*) FROM employees GROUP BY department [2]


15. Difference between WHERE and HAVING

- WHERE filters rows before grouping

- HAVING filters groups after GROUP BY

- WHERE can't use aggregates; HAVING can 


16. What is a JOIN

- Combines rows from two or more tables

- Based on related columns

- INNER JOIN returns matching rows only 


17. Types of JOINs

- INNER JOIN: matching rows

- LEFT JOIN: all from left + matches from right

- RIGHT JOIN: all from right + matches from left

- FULL OUTER JOIN: all from both 


18. What are aggregate functions

- Perform calculations on groups

- COUNT, SUM, AVG, MIN, MAX

- Example: SELECT AVG(salary) FROM employees 


19. What is a subquery

- Query inside another query

- Used in SELECT, WHERE, FROM

- Example: SELECT * FROM users WHERE age > (SELECT AVG(age) FROM users)


20. Interview tip you must remember

- Explain query execution order: FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY

- Use simple examples with 2-3 rows

- Practice on sample datasets like employees/orders 






Post a Comment

0 Comments