Top 50 SQL Interview Questions Most Asked
If you’re gearing up for an interview where SQL is a focus, you’ve come to the right place! As someone who’s navigated the interview process, I know that SQL questions often define how well you perform. Interviewers aim to test not just your ability to write queries but also your understanding of database design, optimization techniques, and real-world problem-solving skills. Expect questions that span from simple SELECT statements to advanced topics like joins, indexing, and performance tuning. These aren’t just theoretical — they’re designed to see how you’d handle real challenges in a professional environment.
In this article, I’ve compiled 50 of the most commonly asked SQL interview questions to give you a competitive edge. Each question is carefully chosen to help you grasp the concepts that matter most and practice the kind of thinking interviewers are looking for. Whether you’re brushing up on the basics or diving into complex scenarios, this guide will prepare you to confidently tackle your next interview. By the time you’re done, you’ll feel more equipped to answer questions, solve problems, and impress your interviewer with your expertise!
1. What is SQL?
SQL, or Structured Query Language, is a standard programming language used to manage and manipulate relational databases. It allows us to interact with databases to store, retrieve, and modify data efficiently. As someone who has worked with SQL, I see it as a tool that bridges the gap between users and databases by providing a straightforward syntax to execute various operations. Whether you’re querying data from a single table or managing complex relationships across multiple tables, SQL serves as the foundation for handling data in a structured format.
What I love about SQL is its versatility. It’s widely used in industries ranging from finance to e-commerce to healthcare. SQL helps you perform operations such as filtering data, aggregating information, and performing complex calculations. Additionally, most relational database management systems, such as MySQL, PostgreSQL, and SQL Server, are built around SQL, making it an essential skill for anyone working with data or applications.
2. What are the uses of SQL?
SQL has numerous applications, and its primary use is to interact with relational databases. I use SQL to query and retrieve specific data from large datasets, which is particularly useful in analytics or reporting tasks. By using simple SQL commands like SELECT
, I can pull out just the information I need, saving time and effort. For example, you can fetch the sales data for the past year from millions of records with a query like this:
SELECT *
FROM Sales
WHERE SalesDate >= '2023-01-01' AND SalesDate <= '2023-12-31';
SQL also plays a critical role in managing and maintaining databases. I use it for tasks like inserting new records, updating existing ones, and even deleting outdated information. Beyond basic data operations, SQL is often utilized to control database access, define user roles, and ensure data consistency through constraints. It’s an all-in-one solution for both developers and data analysts.
3. Name the subgroups in which SQL commands are divided.
SQL commands are grouped into several subcategories, each serving a specific purpose in database management. From my experience, the five primary subgroups include DDL (Data Definition Language), DML (Data Manipulation Language), DCL (Data Control Language), TCL (Transaction Control Language), and DQL (Data Query Language). Each group addresses a different aspect of database interaction, making it easier to organize and manage operations.
For example, when I need to create or alter database structures like tables, I rely on DDL commands. For data insertion or modification, DML commands come into play. To control user access, DCL commands are invaluable. These subgroups ensure that SQL provides a structured way to handle both data and databases effectively, no matter the use case.
4. What is DDL?
Data Definition Language (DDL) consists of commands used to define the structure of a database, such as creating, modifying, or deleting tables, schemas, and other database objects. From my perspective, DDL commands are essential when setting up a new database or making significant changes to its architecture. These commands help you outline how your data will be organized and stored, ensuring a logical structure for future use.
5. Name all the DDL commands.
The main DDL commands include CREATE, ALTER, DROP, TRUNCATE, and RENAME. Each of these serves a distinct purpose in defining or modifying database structures. I’ve often used these commands when building or maintaining databases. For example:
- CREATE: Used to create tables, indexes, or schemas.
- ALTER: Allows modification of an existing database object, like adding a new column.
- DROP: Permanently removes objects like tables or schemas.
- TRUNCATE: Deletes all records from a table but retains its structure.
- RENAME: Renames an existing database object.
These commands help you organize your database effectively, making it easier to manage as your project scales. Using these DDL commands has been crucial in my work to maintain robust and scalable database systems.
6. What is DML?
Data Manipulation Language (DML) is a category of SQL commands used to manipulate the data stored in a database. From my experience, DML commands are essential for performing operations such as inserting new data, updating existing data, and deleting records that are no longer needed. These commands make it possible to manage and modify the contents of a database effectively without altering its structure.
For example, if I need to add a new record to an Employees
table, I can use the INSERT
command:
INSERT INTO Employees (EmployeeID, Name, Department, HireDate)
VALUES (101, 'John Doe', 'IT', '2024-01-01');
In addition to insertion, I frequently use DML commands for updating records with the UPDATE
command or deleting unnecessary data with DELETE
. These operations allow databases to remain dynamic and reflect the most current state of the information.
7. Name all the DML commands.
The key DML commands include INSERT, UPDATE, DELETE, and sometimes SELECT (though SELECT is technically categorized under DQL). Each command plays a vital role in handling database records. Here’s how I’ve used these commands:
- INSERT: Adds new records to a table.
- UPDATE: Modifies existing records to reflect changes.
- DELETE: Removes records that are no longer needed.
For example, to update an employee’s department, I use the UPDATE
command like this:
UPDATE Employees
SET Department = 'HR'
WHERE EmployeeID = 101;
These DML commands are the foundation of day-to-day database operations. They allow you to maintain accurate and relevant data in real time, ensuring that your database remains a reliable source of information.
8. What is DCL?
Data Control Language (DCL) is a set of SQL commands designed to control access to data and define permissions within a database. From my perspective, DCL commands are indispensable when it comes to securing sensitive information and ensuring that only authorized users can perform specific actions. The two main DCL commands, GRANT and REVOKE, help manage user access.
For example, if I want to give a user permission to read and update data in the Employees
table, I use the GRANT
command:
GRANT SELECT, UPDATE
ON Employees
TO 'username';
Similarly, if a user no longer requires access to certain data, the REVOKE
command allows me to withdraw those permissions. This ensures that sensitive data remains protected while providing flexibility for managing user roles. In my experience, DCL commands are critical in environments where multiple users or teams interact with the same database, as they help maintain control and security.
9. Name all the DCL commands.
The main DCL commands in SQL are GRANT and REVOKE. These commands are crucial for managing user access to database objects and ensuring security. In my experience, GRANT is used to assign specific privileges to users, while REVOKE is used to withdraw those privileges. For example, if I need to allow a user to read data from a table, I would use the GRANT
command. Similarly, if their access is no longer required, I can use the REVOKE
command to restrict their permissions.
These commands are essential for maintaining a secure database environment, especially in scenarios where multiple users or teams need different levels of access. Here’s an example of how they work:
GRANT SELECT ON Employees TO 'read_only_user';
REVOKE SELECT ON Employees FROM 'read_only_user';
This way, DCL commands ensure that only authorized users have the necessary privileges to perform specific operations on the database.
10. What is TCL?
Transaction Control Language (TCL) is a subset of SQL used to manage transactions in a database. In my projects, I’ve relied on TCL commands to ensure that a group of database operations either completes successfully or rolls back if any issue occurs. This helps maintain data consistency and integrity.
For example, when performing multiple updates to different tables as part of a single logical operation, TCL commands allow me to control whether the changes are saved permanently (COMMIT
) or reversed (ROLLBACK
) if an error is encountered. Transactions also help prevent partial updates, ensuring that the database remains in a valid state.
11. Name all the TCL commands.
The most commonly used TCL commands are:
- COMMIT: Saves all the changes made in a transaction.
- ROLLBACK: Reverts all the changes in a transaction if something goes wrong.
- SAVEPOINT: Creates a point within a transaction to which you can roll back later.
- SET TRANSACTION: Defines properties for the transaction, such as isolation levels.
Here’s an example demonstrating TCL:
BEGIN TRANSACTION;
UPDATE Employees SET Salary = Salary + 2000 WHERE Department = 'IT';
SAVEPOINT BeforeMarketingUpdate;
UPDATE Employees SET Salary = Salary + 1500 WHERE Department = 'Marketing';
ROLLBACK TO BeforeMarketingUpdate;
COMMIT;
This example adjusts salaries and uses a savepoint to undo changes selectively before committing the transaction.
Basic SQL questions
Question 1: What is SQL?
Question 2: What are the uses of SQL?
Question 3: Name the subgroups in which SQL commands are divided.
Question 4: What is DDL?
Question 5: Name all the DDL commands.
Question 6: What is DML?
Question 7: Name all the DML commands.
Question 8: What is DCL?
9: Name all the DCL commands.
10: What is TCL?
11: Name all the TCL commands.
12: Which clause is used to specify conditions in a SQL query?
13: Suppose we have a table “Employees” with three columns – “name”, “age”, and “location”. Create a new table “NEWEMPLOYEES” and it should contain “name” and “age” of all the records from “Employees”.
14: Suppose we want to create a new table “NEWEMPLOYEES” from an existing table “EMPLOYEES”. We need to add all the records where “age” is greater than 30. How can we do it?
15: What is the difference between primary key and unique key?
16: What is a foreign key?
17: What is the use of the ALTER command?
18: (Missing)
Question 19: (Missing)
Question 20: (Missing)
Question 21: Suppose we have 20 records in the “EMPLOYEES” table. Each row has three columns – “name”, “age”, and “location”. The value of “location” can be the same for multiple records. Write a query to find all distinct values of “location” are there in the “EMPLOYEES” table.
Question 22: Write a query to find the total number of employees who are older than 30 years in the “EMPLOYEES” table.
Question 23: Write a query to find all the names from the “EMPLOYEES” table where the name starts with “a” and ends with “s”.
Question 24: Write a query to find all the records in the “EMPLOYEES” table where the location is either “New York”, “Chicago”, or “Detroit”.
Question 25: Write a query to find all the records in the “EMPLOYEES” table where age is greater than 30 and location is “New York”.
Question 26: Which logical operators can be used in SQL queries?
Question 27: Write a query to get the first 5 records from the “EMPLOYEES” table.
Question 28: Write a query to count the number of records per location in the “EMPLOYEES” table.
Question 29: What is a join in SQL?
Question 30: Name the types of join in SQL.
Question 31: Write a query to find the names of the employees that exist in both tables.
Question 32: Write a query to find the employee ids and names of the employees that exist in “EMPLOYEES” tables and their availability is true.
Question 33: What is the use of self join? Give an example.
Question 34: Which operator is used to find the records in a given range?
Question 35: What is the difference between IN and BETWEEN operators?
Question 36: Write a query to return all the records from the “EMPLOYEES” table but only if an employee name “Mark” whose age is 33 exists in the “ONSITE” table.
Question 37: What is the use of ANY operator?
Question 38: What is the use of ALL operator?
Question 39: Along which clauses the ALL operator is used?
Question 40: When will the EXIST clause return true?
Question 41: Which keyword is used to sort columns?
Question 42: Which command is used to save the work?
Question 43: If there was a mistake and you want to restore the database to the last commit, which command will you use?
Question 44: Which operator is used to check empty values?
Question 45: What are the different types of cursors in SQL?
Question 46: Differentiate between SQL and NoSQL databases.
Question 47: What is a view?
Question 48: Name the types of views in SQL.
Question 49: Explain normalization. What are its advantages?
Question 50: Explain SQL injection.