Basic SQL Questions
1. What is SQL?
SQL, or Structured Query Language, is a programming language designed for managing and manipulating relational databases. It allows users to perform tasks such as querying data, updating records, inserting new data, and deleting unwanted data.
2. What are the different types of SQL statements?
SQL statements can be categorized into several types:
- DDL (Data Definition Language): These statements define the structure of the database, including commands like `CREATE`, `ALTER`, and `DROP`.
- DML (Data Manipulation Language): These statements are used for data manipulation, including commands like `SELECT`, `INSERT`, `UPDATE`, and `DELETE`.
- DCL (Data Control Language): These are used to control access to data, including commands like `GRANT` and `REVOKE`.
- TCL (Transaction Control Language): These statements manage transactions in the database, including commands like `COMMIT`, `ROLLBACK`, and `SAVEPOINT`.
3. What is a primary key?
A primary key is a column or a set of columns that uniquely identifies each row in a table. It must contain unique values and cannot contain NULL values. The primary key enforces entity integrity.
4. What is a foreign key?
A foreign key is a column or a set of columns in one table that refers to the primary key of another table. It establishes a relationship between the two tables, ensuring referential integrity.
5. Explain the difference between `INNER JOIN` and `OUTER JOIN`.
- INNER JOIN: This join returns only the rows that have matching values in both tables. If there is no match, the rows are omitted.
- OUTER JOIN: This join returns all the rows from one table and the matching rows from the other table. If there is no match, NULL values are returned for the columns of the table without a match. OUTER JOIN can be further categorized into:
- LEFT OUTER JOIN: Returns all rows from the left table and matched rows from the right table.
- RIGHT OUTER JOIN: Returns all rows from the right table and matched rows from the left table.
- FULL OUTER JOIN: Returns all rows when there is a match in either left or right table records.
Intermediate SQL Questions
6. What is normalization? Explain its types.
Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. The main types of normalization are:
- First Normal Form (1NF): Ensures that all columns contain atomic values and each column contains only one value per row.
- Second Normal Form (2NF): Achieved when a table is in 1NF and all non-key attributes are fully functionally dependent on the primary key.
- Third Normal Form (3NF): A table is in 3NF if it is in 2NF and all its attributes are functionally dependent only on the primary key.
7. What is an index? Why is it used?
An index is a database object that improves the speed of data retrieval operations on a database table. It is created on a table's columns and helps to quickly locate data without scanning the entire table. However, indexes can slow down data modification operations (inserts, updates, deletes) because the index must also be updated.
8. What is a subquery? Provide an example.
A subquery is a query nested inside another SQL statement. It can be used in various clauses like `SELECT`, `WHERE`, and `FROM`. Subqueries can return a single value or a set of values.
Example:
```sql
SELECT employee_id, employee_name
FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1000);
```
9. What are aggregate functions in SQL?
Aggregate functions perform a calculation on a set of values and return a single value. Common aggregate functions include:
- `COUNT()`: Returns the number of rows that match a specified condition.
- `SUM()`: Returns the total sum of a numeric column.
- `AVG()`: Returns the average value of a numeric column.
- `MAX()`: Returns the maximum value in a set.
- `MIN()`: Returns the minimum value in a set.
10. Explain the use of `GROUP BY` and `HAVING` clauses.
- GROUP BY: This clause groups rows that have the same values in specified columns into summary rows, like "total sales by department."
- HAVING: This clause filters records that work on summarized `GROUP BY` results. It is often used with aggregate functions.
Example:
```sql
SELECT department_id, COUNT() AS employee_count
FROM employees
GROUP BY department_id
HAVING COUNT() > 10;
```
Advanced SQL Questions
11. What is a stored procedure?
A stored procedure is a set of SQL statements that can be stored in the database and executed as needed. Stored procedures can accept parameters and are used to encapsulate complex logic, improve performance, and enhance security.
12. What is a trigger? How does it differ from a stored procedure?
A trigger is a special type of stored procedure that automatically executes or fires in response to certain events on a particular table, such as `INSERT`, `UPDATE`, or `DELETE`. The main difference is that triggers are event-driven, while stored procedures are called explicitly.
13. Explain the concept of transactions in SQL.
A transaction is a sequence of one or more SQL operations that are performed as a single unit of work. Transactions ensure data integrity and consistency, and they follow the ACID properties:
- Atomicity: Ensures that all operations within the transaction are completed successfully or none at all.
- Consistency: Ensures that the database transitions from one valid state to another.
- Isolation: Ensures that transactions are securely and independently processed without interference.
- Durability: Ensures that once a transaction is committed, it remains so, even in the event of a system failure.
14. What is the difference between `UNION` and `UNION ALL`?
- UNION: Combines the results of two or more SELECT statements and removes duplicate rows from the result set.
- UNION ALL: Combines the results of two or more SELECT statements but includes all rows in the result set, including duplicates.
15. Can you explain the concept of window functions?
Window functions are used to perform calculations across a set of table rows related to the current row. Unlike aggregate functions that return a single value, window functions return a value for each row while keeping the original row structure.
Example:
```sql
SELECT employee_id, salary, AVG(salary) OVER (PARTITION BY department_id) AS avg_salary
FROM employees;
```
Conclusion
SQL interview questions are varied and cover a wide range of topics, from basic definitions to complex queries and transaction management. Mastering these concepts is vital for anyone looking to work with databases effectively. By preparing for these questions, candidates can demonstrate their SQL skills and their ability to leverage data in their future roles. Understanding the underlying principles and being able to articulate them clearly will greatly enhance your performance in SQL interviews.
Frequently Asked Questions
What is SQL and why is it used?
SQL, or Structured Query Language, is a standard programming language specifically designed for managing and manipulating relational databases. It is used to perform tasks such as querying data, updating records, inserting new data, and deleting existing data.
What are the different types of SQL commands?
SQL commands are generally categorized into five types: Data Query Language (DQL), Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL). DQL includes SELECT statements, DDL includes CREATE and DROP statements, DML includes INSERT and UPDATE statements, DCL includes GRANT and REVOKE, and TCL includes COMMIT and ROLLBACK.
What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN returns only the rows that have matching values in both tables involved in the join, while LEFT JOIN returns all rows from the left table and the matched rows from the right table. If there is no match, NULL values are returned for columns from the right table.
What is normalization and why is it important in SQL?
Normalization is the process of organizing a database to reduce redundancy and improve data integrity. It involves dividing a database into multiple tables and defining relationships between them. Normalization is important because it minimizes the potential for data anomalies and ensures that the database is efficient and easy to maintain.
What is the purpose of the GROUP BY clause in SQL?
The GROUP BY clause is used in SQL to arrange identical data into groups. It is often used with aggregate functions like COUNT, SUM, AVG, MAX, and MIN to produce summary reports. For example, grouping sales data by customer ID allows the user to analyze total sales per customer.