Top 25+ SQL Interview Q&A with Practical Test | Try Personalized AI Help

Join our community to see how developers are using Workik AI everyday.

Interview Question & Answers for SQL Developers

Q1: What is SQL, and what are its main functions?

SQL (Structured Query Language) is a standardized language used to communicate with relational databases. Its main functions include:

  • Data Definition Language (DDL): Commands like CREATE , ALTER , DROP are used to define and manage database schema and objects.
  • Data Manipulation Language (DML): Commands like SELECT , INSERT , UPDATE , DELETE are used to retrieve and manipulate data.
  • Data Control Language (DCL): Commands like GRANT , REVOKE are used to control access to data.
  • Transaction Control Language (TCL): Commands like COMMIT , ROLLBACK , SAVEPOINT are used to manage transactions in the database.

Q2: What are the different types of joins in SQL?

Joins are used to combine rows from two or more tables based on a related column between them. The main types of joins are:

  • INNER JOIN: Returns records that have matching values in both tables.
  • SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
  • LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table and matched records from the right table. The result is NULL from the right side if there is no match.
  • SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
  • RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table and matched records from the left table. The result is NULL from the left side if there is no match.
  • SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;
  • FULL JOIN (or FULL OUTER JOIN): Returns all records when there is a match in either left or right table records.
  • SELECT * FROM table1 FULL JOIN table2 ON table1.id = table2.id;
  • CROSS JOIN: Returns the Cartesian product of the two tables.
  • SELECT * FROM table1 CROSS JOIN table2;

Q3: What is normalization? Explain its various forms.

Normalization is a database design technique used to reduce redundancy and dependency by organizing fields and tables of a database. Its various forms (normal forms) include:

  • First Normal Form (1NF): Ensures each column contains atomic (indivisible) values and each column contains values of a single type.
  • Second Normal Form (2NF): Achieves 1NF and ensures that all non-key attributes are fully functional dependent on the primary key.
  • Third Normal Form (3NF): Achieves 2NF and ensures that all the attributes are only dependent on the primary key.
  • Boyce-Codd Normal Form (BCNF): A stricter version of 3NF where every determinant must be a candidate key.
  • Fourth Normal Form (4NF): Achieves BCNF and ensures no multi-valued dependencies other than a candidate key.
  • Fifth Normal Form (5NF): Achieves 4NF and ensures that there are no join dependencies.

Q4: What are indexes in SQL? Give their types.

Indexes are special lookup tables that the database search engine can use to speed up data retrieval. An index is a pointer to data in a table. Types of indexes include:

  • Unique Index: Ensures that the indexed field will have unique values.
  • CREATE UNIQUE INDEX idx_name ON table(column);
  • Clustered Index: Reorders the physical order of the table and searches using the key values. Each table can have only one clustered index.
  • CREATE CLUSTERED INDEX idx_name ON table(column);
  • Non-clustered Index: Does not alter the physical order of the table and maintains a logical order of the data. A table can have multiple non-clustered indexes.
  • CREATE INDEX idx_name ON table(column);
  • Composite Index: An index on two or more columns of a table.
  • CREATE INDEX idx_name ON table(column1, column2);

Q5: What is a primary key, and how is it different from a unique key?

A primary key is a field (or a combination of fields) in a table that uniquely identifies each row/record in that table. The primary key has the following characteristics:

  • Uniqueness: Each value in the primary key column(s) must be unique across the table. This ensures that each row can be uniquely identified.
  • Not Null: A primary key column cannot contain NULL values. Every record must have a value for the primary key.
  • Single Instance: A table can have only one primary key, which can consist of single or multiple columns (composite key).
  • CREATE TABLE Employees (
        EmployeeID INT PRIMARY KEY,
        FirstName VARCHAR(50),
        LastName VARCHAR(50)
    );

A unique key, on the other hand, also enforces uniqueness of the values in a column or a set of columns, but it differs from the primary key in the following ways:

  • Multiple Unique Keys: A table can have multiple unique keys in addition to the primary key.
  • NULL Values: A unique key can contain NULL values, although in many SQL implementations, only a single NULL value is allowed in a column with a unique constraint.
  • CREATE TABLE Employees (
        EmployeeID INT PRIMARY KEY,
        Email VARCHAR(100) UNIQUE,
        FirstName VARCHAR(50),
        LastName VARCHAR(50)
    );

Request question

Please fill in the form below to submit your question.

Q6: What is a foreign key, and why is it used?

A foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table. The purpose of the foreign key is to ensure referential integrity of the data, meaning that the foreign key values in the referencing table must match primary key values in the referenced table.

Uses:

  • Data Integrity: Ensures that relationships between tables remain consistent. If a record in the parent table (referenced table) is deleted, the corresponding records in the child table (referencing table) can either be deleted or set to NULL, depending on the referential action specified.
  • Joining Tables: Facilitates joining tables in queries to retrieve related data across multiple tables.
  • CREATE TABLE Orders (
        OrderID INT PRIMARY KEY,
        OrderDate DATE,
        CustomerID INT,
        FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
    );

Q7: What is the difference between DELETE, TRUNCATE, and DROP commands?

These commands are used to remove data or database objects but differ in their operations and consequences:

  • DELETE:
    • Usage: Deletes rows from a table based on a specified condition.
    • Transaction: DML command that can be rolled back if used within a transaction.
    • Performance: Slower than TRUNCATE because it logs individual row deletions.
    • DELETE FROM table_name WHERE condition;
  • TRUNCATE:
    • Usage: Removes all rows from a table but retains the table structure for future use.
    • Transaction: DDL command that cannot be rolled back in most systems.
    • Performance: Faster than DELETE as it deallocates data pages instead of logging individual row deletions.
    • TRUNCATE TABLE table_name;
  • DROP:
    • Usage: Deletes the entire table, including its structure, from the database.
    • Transaction: DDL command that cannot be rolled back in most systems.
    • Performance: Irreversible and completely removes the table and its data.
    • DROP TABLE table_name;

Q8: What are stored procedures, and what are their benefits?

Stored procedures are precompiled collections of one or more SQL statements stored under a name and processed as a unit. They can accept parameters, perform operations, and return results.

Benefits:

  • Performance: Precompiled and cached, which reduces the parsing time for queries.
  • Security: Can encapsulate business logic, providing controlled access to data.
  • Reusability: Can be reused across different applications and users, promoting code reuse.
  • Maintainability: Easier to maintain since changes can be made in one place without altering multiple queries.
  • CREATE PROCEDURE GetEmployeeDetails
        @EmployeeID INT
    AS
    BEGIN
        SELECT * FROM Employees WHERE EmployeeID = @EmployeeID;
    END;

Q9: Explain ACID properties in the context of a database transaction.

ACID properties ensure reliable processing of database transactions and maintain data integrity even in the event of failures. The properties are:

  • Atomicity: Ensures that all operations within a transaction are completed successfully. If any operation fails, the entire transaction is rolled back.
  • Consistency: Ensures that a transaction takes the database from one valid state to another valid state, maintaining database integrity.
  • Isolation: Ensures that the operations of a transaction are isolated from those of other transactions. This prevents transactions from interfering with each other.
  • Durability: Ensures that the results of a committed transaction are permanent and survive system failures.
  • BEGIN TRANSACTION;
        UPDATE Accounts SET Balance = Balance - 100 WHERE AccountID = 1;
        UPDATE Accounts SET Balance = Balance + 100 WHERE AccountID = 2;
    IF @@ERROR <> 0
        ROLLBACK TRANSACTION;
    ELSE
        COMMIT TRANSACTION;

Q10: What is a common table expression (CTE), and how is it used?

A common table expression (CTE) is a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, or DELETE statement. CTEs improve readability and simplify complex queries by breaking them into simpler parts.

Usage:

  • Recursive Queries: Performing recursive operations, such as traversing hierarchical data.
  • Simplifying Queries: Breaking down complex queries into more manageable parts.
  • WITH EmployeeCTE AS (
        SELECT EmployeeID, FirstName, LastName, ManagerID
        FROM Employees
        WHERE ManagerID IS NULL
        UNION ALL
        SELECT e.EmployeeID, e.FirstName, e.LastName, e.ManagerID
        FROM Employees e
        INNER JOIN EmployeeCTE ecte ON e.ManagerID = ecte.EmployeeID
    )
    SELECT * FROM EmployeeCTE;

Request question

Please fill in the form below to submit your question.

Q11: What is a view in SQL, and why would you use it?

A view is a virtual table that is based on the result set of an SQL query. It contains rows and columns, just like a real table, and the fields in a view are fields from one or more real tables in the database.

Uses of Views:

  • Simplify Complex Queries: Encapsulates complex queries in a single view, making it easier to query.
  • Security: Restricts access to specific rows and columns of data by allowing users to query the view instead of the underlying table.
  • Data Abstraction: Provides a layer of abstraction, presenting a consistent, unchanging interface even if the underlying database schema changes.
  • Reusability: Once created, views can be reused across multiple queries.
  • CREATE VIEW EmployeeView AS
    SELECT EmployeeID, FirstName, LastName, Department
    FROM Employees
    WHERE Department = 'Sales';

Q12: What is a trigger in SQL, and when would you use it?

A trigger is a special kind of stored procedure that automatically executes in response to certain events on a particular table or view. Triggers can be used to enforce business rules, maintain audit trails, and synchronize tables.

Types of Triggers:

  • Before Triggers: Execute before an INSERT, UPDATE, or DELETE operation.
  • After Triggers: Execute after an INSERT, UPDATE, or DELETE operation.
  • Instead of Triggers: Execute in place of the triggering operation, commonly used with views.
  • CREATE TRIGGER trgAfterInsert
    ON Employees
    AFTER INSERT
    AS
    BEGIN
        INSERT INTO EmployeeAudit(EmployeeID, ChangeDate)
        SELECT EmployeeID, GETDATE()
        FROM inserted;
    END;

Uses:

  • Enforcing Business Rules: Automatically enforce complex business rules that cannot be enforced by constraints.
  • Audit Trails: Automatically log changes to data for auditing purposes.
  • Data Synchronization: Ensure data consistency across related tables.

Q13: Explain the concept of a subquery in SQL.

A subquery, also known as an inner query or nested query, is a query within another SQL query. The result of the subquery is used by the outer query. Subqueries can be used in various SQL clauses like SELECT, INSERT, UPDATE, DELETE, and WHERE.

Types of Subqueries:

  • Single-Row Subquery: Returns a single row of results.
  • SELECT * FROM Employees
    WHERE EmployeeID = (SELECT ManagerID FROM Departments WHERE DepartmentID = 1);
  • Multi-Row Subquery: Returns multiple rows of results.
  • SELECT * FROM Employees
    WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE Location = 'New York');
  • Correlated Subquery: A subquery that references columns from the outer query.
  • SELECT e1.EmployeeID, e1.FirstName
    FROM Employees e1
    WHERE e1.Salary > (SELECT AVG(e2.Salary) FROM Employees e2 WHERE e2.DepartmentID = e1.DepartmentID);

Q14: What is the difference between UNION and UNION ALL?

UNION: Combines the result sets of two or more SELECT queries into a single result set, excluding duplicate rows. The columns in the SELECT statements must have the same number and compatible data types.

SELECT City FROM Customers
UNION
SELECT City FROM Suppliers;

UNION ALL: Combines the result sets of two or more SELECT queries into a single result set, including all duplicate rows.

SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers;

Key Differences:

  • Duplicates: UNION removes duplicate rows, while UNION ALL includes all duplicates.
  • Performance: UNION is slower due to the additional step of removing duplicates, while UNION ALL is faster as it simply combines the results.

Q15: What are aggregate functions in SQL? Provide examples.

Aggregate functions perform a calculation on a set of values and return a single value. They are commonly used with the GROUP BY clause in SQL queries to group rows that have the same values in specified columns.

Common Aggregate Functions:

  • COUNT(): Returns the number of rows.
  • SELECT COUNT(*) FROM Employees;
  • SUM(): Returns the total sum of a numeric column.
  • SELECT SUM(Salary) FROM Employees;
  • AVG(): Returns the average value of a numeric column.
  • SELECT AVG(Salary) FROM Employees;
  • MIN(): Returns the minimum value.
  • SELECT MIN(Salary) FROM Employees;
  • MAX(): Returns the maximum value.
  • SELECT MAX(Salary) FROM Employees;

Request question

Please fill in the form below to submit your question.

Q16: What is a materialized view, and how is it different from a regular view?

A materialized view is a database object that contains the results of a query and stores them physically. Unlike a regular view, which is a virtual table created dynamically upon each access, a materialized view stores data and can be refreshed periodically.

Differences:

  • Storage: Regular views do not store data physically; materialized views store data physically.
  • Performance: Materialized views can improve query performance as the data is precomputed and stored. Regular views are slower as they execute the underlying query each time they are accessed.
  • Update Frequency: Materialized views need to be refreshed to reflect the latest data changes, whereas regular views always show the current data from the underlying tables.

Q17: What are user-defined functions (UDFs) in SQL, and how are they different from stored procedures?

User-defined functions (UDFs) are routines that accept parameters, perform an action, and return the result of that action as a value. UDFs can be scalar (return a single value) or table-valued (return a table).

Differences from Stored Procedures:

  • Return Type: UDFs must return a value, whereas stored procedures may or may not return values.
  • Usage in Queries: UDFs can be used in SQL statements like SELECT, WHERE, and JOIN clauses, while stored procedures cannot.
  • Transaction Control: UDFs do not allow explicit transaction control (BEGIN TRANSACTION, COMMIT, ROLLBACK), whereas stored procedures do.

Q18: What is database sharding, and why is it used?

Database sharding is a method of distributing data across multiple databases or servers to improve performance and scalability. Each shard is a separate database that contains a subset of the data.

Uses:

  • Scalability: Allows the database to handle more data and transactions by distributing the load across multiple servers.
  • Performance: Reduces the amount of data each server has to handle, improving query response times.
  • Availability: Enhances availability by isolating failures to individual shards.
  • Example: If a customer database is sharded by region, one shard might contain data for North America, while another shard contains data for Europe.

Q19: What is the difference between HAVING and WHERE clauses in SQL?

The HAVING and WHERE clauses are both used to filter records in SQL, but they operate in different contexts and stages of query execution.

WHERE Clause:

  • Purpose: Filters rows before any groupings are made.
  • Usage: Applied to individual rows in a table.
  • Context: Used in SELECT, UPDATE, DELETE statements.
  • SELECT * FROM Employees WHERE Salary > 50000;

HAVING Clause:

  • Purpose: Filters groups after the GROUP BY clause has been applied.
  • Usage: Applied to aggregated data.
  • Context: Used with GROUP BY in SELECT statements.
  • SELECT Department, COUNT(*) as EmployeeCount
    FROM Employees
    GROUP BY Department
    HAVING COUNT(*) > 10;

Key Differences:

  • Filtering Stage: WHERE filters before grouping, HAVING filters after grouping.
  • Applicability: WHERE cannot be used with aggregate functions; HAVING can.

Q20: What is a surrogate key in SQL?

A surrogate key is an artificial key that is used as a unique identifier for each row in a table. It is not derived from the data in the table and has no business meaning. Surrogate keys are typically auto-incremented integers.

Uses:

  • Uniqueness: Ensures each row has a unique identifier.
  • Simplicity: Simplifies the identification of rows, especially in cases where there are no natural unique keys.
  • CREATE TABLE Employees (
        EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
        FirstName VARCHAR(50),
        LastName VARCHAR(50)
    );

Request question

Please fill in the form below to submit your question.

Practical Assessment Based Questions & Answer

Q1: You have the following SQL query that is supposed to retrieve the names of employees who have been hired after January 1, 2023. However, the query is not returning any results. Identify and correct the error.
SELECT FirstName, LastName
FROM Employees
WHERE HireDate > '01-01-2023';

The date format is incorrect and should be in the correct SQL date format (YYYY-MM-DD).

SELECT FirstName, LastName
FROM Employees
WHERE HireDate > '2023-01-01';
Q2: You have a query that retrieves data from a large table. The query is taking too long to execute. Here is the query:
SELECT * 
FROM Orders
WHERE CustomerID = 12345;

Answer: To improve performance, you can create an index on the CustomerID column.

CREATE INDEX idx_customerid ON Orders(CustomerID);

Q3: Write a SQL query to find the third highest salary from the Employees table.

SELECT Salary
FROM (
    SELECT Salary, DENSE_RANK() OVER (ORDER BY Salary DESC) AS rank
    FROM Employees
) AS RankedSalaries
WHERE rank = 3;
Q4: What will be the output of the following query?
SELECT EmployeeID, FirstName, LastName
FROM Employees
WHERE EmployeeID IN (SELECT ManagerID FROM Employees);

The output will be a list of employees who are also managers. It includes the EmployeeID, FirstName, and LastName of those employees whose EmployeeID matches any ManagerID in the Employees table.

Q5: Optimize the following query that retrieves employee details along with their department names. The Employees table has millions of rows.
SELECT E.EmployeeID, E.FirstName, E.LastName, D.DepartmentName
FROM Employees E
JOIN Departments D ON E.DepartmentID = D.DepartmentID;

Ensure that there are indexes on the join columns to optimize performance.

CREATE INDEX idx_employee_departmentid ON Employees(DepartmentID);
CREATE INDEX idx_department_departmentid ON Departments(DepartmentID);
Q6: The following query to delete employees who have not logged in for more than a year is not working. Identify the error.

DELETE FROM Employees
WHERE LastLogin < DATEADD(YEAR, -1, GETDATE());

The query is correct. Ensure that the LastLogin column is of date type and contains valid dates. If using SQL Server, the function is correct, but if using another SQL dialect, you might need to adjust the date function. For example, in MySQL, you would use ‘DATE_SUB’.

Q7: Rewrite the following query to use a CTE (Common Table Expression) for better readability and maintainability.
SELECT DepartmentID, AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY DepartmentID
ORDER BY AvgSalary DESC;

Using a Common Table Expression (CTE) named DepartmentSalaries improves readability by breaking the query into two parts: one that calculates the average salary for each department and one that selects and orders the results. This separation makes the query easier to understand and maintain.

WITH DepartmentSalaries AS (
    SELECT DepartmentID, AVG(Salary) AS AvgSalary
    FROM Employees
    GROUP BY DepartmentID
)
SELECT DepartmentID, AvgSalary
FROM DepartmentSalaries
ORDER BY AvgSalary DESC;
Q8: The following query should return the total sales for each store, but it returns an incorrect result. Identify and correct the error.
SELECT StoreID, SUM(SalesAmount) AS TotalSales
FROM Sales
GROUP BY StoreID
ORDER BY TotalSales DESC;

The query is correct. Ensure that the SalesAmount column does not contain NULL values and that the data types are consistent.

SELECT StoreID, SUM(SalesAmount) AS TotalSales
FROM Sales
GROUP BY StoreID
ORDER BY TotalSales DESC;
Q9: You have a query that calculates the total sales for each month. It is running slowly. Optimize the query.
SELECT MONTH(SaleDate) AS SaleMonth, SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY MONTH(SaleDate);

Create an index on the SaleDate column to improve performance.

CREATE INDEX idx_saledate ON Sales(SaleDate);
Q10: Write a SQL query to find employees who have the same last name as their managers. Assume the Employees table has a column ManagerID.
SELECT E1.EmployeeID, E1.FirstName, E1.LastName
FROM Employees E1
JOIN Employees E2 ON E1.ManagerID = E2.EmployeeID
WHERE E1.LastName = E2.LastName;

Request question

Please fill in the form below to submit your question.

Discover the True Potential of AI in Development with Workik

Join developers who are using Workik’s AI assistance everyday for programming

Sign Up Now

Overview of SQL

What is SQL?

What is the history and latest trends in SQL development?

What are popular Database Management Systems in SQL?

  • MySQL: An open-source relational database management system.
  • PostgreSQL: An open-source, object-relational database system known for its extensibility and standards compliance.
  • SQLite: A C-language library that provides a lightweight, disk-based database.
  • Microsoft SQL Server: A relational database management system developed by Microsoft.
  • Oracle Database: A multi-model database management system produced and marketed by Oracle Corporation.

What are the use cases of SQL?

  • Database Management: Creating, updating, and deleting database tables and their relationships.
  • Data Analysis: Querying and analyzing large datasets, often used in business intelligence tools.
  • Web Development: Managing data in web applications, ensuring data integrity and security.
  • ETL Processes: Used in Extract, Transform, Load processes for data warehousing.
  • Reporting: Generating reports from data stored in relational databases.

What are some of the tech roles associated with SQL expertise?

  • Database Administrator (DBA): Responsible for the performance, integrity, and security of a database.
  • Data Analyst: Uses SQL to extract and analyze data to help inform business decisions.
  • Backend Developer: Develops server-side logic, integrates SQL databases, and manages data flow.
  • Business Intelligence Developer: Designs and builds data models, reports, and dashboards using SQL.
  • Data Engineer: Focuses on the development and maintenance of data architectures, including databases and large-scale processing systems.

What pay package can be expected with experience in SQL?

  • Junior SQL Developer (0-2 years experience): $55,000 - $75,000 per year.
  • Mid-Level SQL Developer (3-5 years experience): $75,000 - $100,000 per year.
  • Senior SQL Developer (5+ years experience): $100,000 - $130,000 per year.
  • Database Administrator (DBA): $85,000 - $120,000 per year.
  • Data Analyst: $60,000 - $90,000 per year.
  • Data Engineer: $95,000 - $140,000 per year.