SQL interview questions
SQL interview questions
What is SQL?
SQL (Structured Query Language) is a programming language used to interact with relational
databases. It helps in managing, querying, and manipulating data effectively.
Uses of SQL
Ensure Data Integrity: Enforce constraints like PRIMARY KEY and FOREIGN KEY.
Perform Analysis: Use aggregate functions (SUM, AVG) and GROUP BY for reports.
o DDL (Data Definition Language), DML (Data Manipulation Language), DCL (Data
Control Language), TCL (Transaction Control Language), and DQL (Data Query
Language).
A Foreign Key is a column in one table that refers to the Primary Key of another table,
creating a relationship between the two tables and ensuring data integrity.
Key Features
2. Data Validation: Ensures values in the foreign key column exist in the referenced table.
3. Cascading Actions: Supports ON DELETE and ON UPDATE rules (e.g., CASCADE, SET NULL).
Example
sql
CopyEdit
dept_name VARCHAR(50)
);
Child Table: Employees
sql
CopyEdit
emp_name VARCHAR(100),
dept_id INT,
);
o INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, CROSS JOIN, SELF JOIN.
Normalization in SQL
Types of Normalization
o Example: Split tables to ensure all non-key attributes depend on the whole primary
key.
o Removes transitive dependency (non-key columns should not depend on other non-
key columns).
o Example: Remove columns that depend on other non-key columns (e.g., location
depending on department).
o Example: Split tables where a non-candidate key column determines other columns.
Summary
2. Simplify Queries: To simplify complex queries that require data from multiple tables.
3. Reporting: In data warehouses or OLAP systems for faster reporting and aggregation.
Key takeaway: Denormalization is used to optimize for faster reads but may lead to
redundancy and maintenance challenges.
Key Difference:
Alias: A temporary name for columns or tables to make queries easier to read.
Subquery: A nested query used to retrieve data that will be used by the outer query
Key Difference:
View: Virtual, does not store data, used for presenting or simplifying queries.
1. RANK():
Same values get the same rank, but subsequent rows have skipped ranks.
2. DENSE_RANK():
Same values get the same rank, but no rank is skipped.
3. ROW_NUMBER():
Every row gets a unique number, no matter if values are the same.
A CTE (Common Table Expression) is a temporary result set in SQL that you can reference
within a SELECT, INSERT, UPDATE, or DELETE statement. It is defined using the WITH clause,
and it improves query readability and maintainability, especially for complex queries.
2. Readability: Helps break down complex queries into simpler, more manageable parts.
3. Recursion: CTEs can be recursive, which is useful for querying hierarchical data (e.g.,
organizational charts, bill of materials).
4. Reference: You can reference the CTE multiple times in the main query.
Syntax:
sql
CopyEdit
WITH CTE_Name AS (
FROM table
WHERE condition
Example:
sql
CopyEdit
WITH TopEmployees AS (
FROM Employees
)
SELECT * FROM TopEmployees;
Explanation: The WITH clause creates a CTE called TopEmployees, which contains employees
earning more than 50,000. The main query then selects all the records from this CTE.
CTEs can be self-referenced and used multiple times in the same query, whereas subqueries
are executed only once and are embedded in the main query.
10. Explain the difference between a CROSS JOIN and an INNER JOIN.
1. How does indexing affect performance? When should you use or avoid it?
10. How do you write a query to find the second-highest salary in a table?
sql
CopyEdit
SELECT MAX(salary)
FROM employees
Scenario-Based Questions
sql
CopyEdit
FROM table_name
3. Write a query to find customers who have made more than 5 purchases.
4. How would you design a query to retrieve the top N records from a table?
sql
CopyEdit
6. Write a query to find employees with the highest bonus in each department.
sql
CopyEdit
FROM employees
GROUP BY department_id;
7. How would you delete duplicate rows from a table?
4. Write a query to group data by month and calculate the total revenue.
5. Explain how you would use SQL for A/B testing analysis.