You are currently viewing Top 10 SQL Interview Questions and Answers

Top 10 SQL Interview Questions and Answers

Structured Query Language (SQL) is a critical skill for anyone working in data management, database administration, or development. To help you prepare for your next SQL interview, here are the top 10 SQL interview questions with detailed answers.

1. What is SQL? Explain its uses.

Answer:
SQL (Structured Query Language) is a standardized programming language used for managing relational databases and performing various operations on the data within them. Its primary uses include:

  • Querying Data: Extracting data from databases using SELECT statements.
  • Updating Data: Modifying existing data using UPDATE statements.
  • Inserting Data: Adding new data into tables using INSERT statements.
  • Deleting Data: Removing data from tables using DELETE statements.
  • Schema Creation and Modification: Creating and altering database objects such as tables, indexes, and views using DDL (Data Definition Language) statements like CREATE, ALTER, and DROP.
  • Access Control: Managing user permissions and ensuring data security through GRANT and REVOKE statements.

2. What are the different types of SQL commands?

Answer:
SQL commands are categorized into five primary types:

  • DDL (Data Definition Language): Used to define and manage database structures.
  • CREATE: Creates a new database object (e.g., table, index).
  • ALTER: Modifies an existing database object.
  • DROP: Deletes a database object.
  • TRUNCATE: Removes all rows from a table, but the table structure remains.
  • DML (Data Manipulation Language): Used for managing data within schema objects.
  • SELECT: Retrieves data from the database.
  • INSERT: Adds new data into a table.
  • UPDATE: Modifies existing data within a table.
  • DELETE: Removes data from a table.
  • DCL (Data Control Language): Deals with rights, permissions, and other controls.
  • GRANT: Gives user access privileges to the database.
  • REVOKE: Takes back privileges granted to a user.
  • TCL (Transaction Control Language): Manages transactions within a database.
  • COMMIT: Saves the transaction changes.
  • ROLLBACK: Reverts the transaction changes.
  • SAVEPOINT: Sets a savepoint within a transaction.
  • DQL (Data Query Language): Primarily used for querying the database.
  • SELECT: Retrieves data from the database.

3. What is a primary key in SQL? Why is it important?

Answer:
A primary key is a column (or a combination of columns) in a table that uniquely identifies each row in that table. It is important because:

  • Uniqueness: Ensures that each record in the table is unique.
  • Non-Null: Primary key columns cannot contain NULL values, ensuring that every row has a valid identifier.
  • Indexing: Most database systems automatically create an index on the primary key, which helps improve query performance.
  • Data Integrity: Helps maintain the integrity and consistency of the data by preventing duplicate and null entries.

Example:

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    Department VARCHAR(50)
);

4. What are joins in SQL? Explain different types of joins.

Answer:
Joins are SQL operations used to combine rows from two or more tables based on related columns. The different types of joins include:

  • INNER JOIN: Returns only the rows that have matching values in both tables.
  SELECT Employees.FirstName, Departments.DepartmentName
  FROM Employees
  INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
  • LEFT JOIN (LEFT OUTER JOIN): Returns all rows from the left table and the matched rows from the right table. If no match is found, NULL values are returned for columns from the right table.
  SELECT Employees.FirstName, Departments.DepartmentName
  FROM Employees
  LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
  • RIGHT JOIN (RIGHT OUTER JOIN): Returns all rows from the right table and the matched rows from the left table. If no match is found, NULL values are returned for columns from the left table.
  SELECT Employees.FirstName, Departments.DepartmentName
  FROM Employees
  RIGHT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
  • FULL JOIN (FULL OUTER JOIN): Returns rows when there is a match in one of the tables. It returns all rows from the left table and the right table, with NULLs in places where the join condition is not met.
  SELECT Employees.FirstName, Departments.DepartmentName
  FROM Employees
  FULL JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
  • CROSS JOIN: Returns the Cartesian product of the two tables, i.e., all possible combinations of rows.
  SELECT Employees.FirstName, Departments.DepartmentName
  FROM Employees
  CROSS JOIN Departments;

5. What is normalization? Explain its different forms.

Answer:
Normalization is the process of organizing the columns and tables of a relational database to minimize redundancy and dependency. Its main objectives are to ensure data integrity and optimize query performance. The different normal forms include:

  • First Normal Form (1NF): Ensures that the table has no repeating groups of columns or arrays. Each column must contain atomic (indivisible) values.
  • Second Normal Form (2NF): Achieved when the table is in 1NF and all non-key columns are fully functionally dependent on the primary key.
  • Third Normal Form (3NF): Achieved when the table is in 2NF and all the columns are not transitively dependent on the primary key.
  • Boyce-Codd Normal Form (BCNF): A stronger version of 3NF where every determinant is a candidate key.
  • Fourth Normal Form (4NF): Achieved when the table is in BCNF and has no multi-valued dependencies.
  • Fifth Normal Form (5NF): Achieved when the table is in 4NF and has no join dependencies.

Example:

  • 1NF Example:
  CREATE TABLE Orders (
      OrderID INT,
      CustomerName VARCHAR(100),
      ProductName VARCHAR(100)
  );
  • 2NF Example:
  CREATE TABLE Orders (
      OrderID INT PRIMARY KEY,
      CustomerID INT,
      OrderDate DATE
  );

  CREATE TABLE Customers (
      CustomerID INT PRIMARY KEY,
      CustomerName VARCHAR(100)
  );

  CREATE TABLE OrderDetails (
      OrderID INT,
      ProductID INT,
      Quantity INT
  );

6. What is a foreign key in SQL?

Answer:
A foreign key is a column or a group of columns in a table that creates a link between data in two tables. It acts as a cross-reference between tables because it references the primary key of another table, ensuring referential integrity.

Importance:

  • Data Consistency: Ensures that the value in the foreign key column(s) must match one of the values in the referenced primary key column(s).
  • Cascade Operations: Can automatically perform updates or deletes in child tables based on the changes in the parent table.

Example:

CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    OrderDate DATE,
    FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

7. What is an index in SQL? Why is it used?

Answer:
An index is a database object that improves the speed of data retrieval operations on a table at the cost of additional storage space and write performance. It is similar to an index in a book, which allows quick lookup of information without having to scan every page.

Types of Indexes:

  • Clustered Index: Sorts and stores the data rows of the table based on the index key. Each table can have only one clustered index.
  • Non-Clustered Index: Creates a separate structure within the table that stores only the key values and pointers to the actual data rows. Tables can have multiple non-clustered indexes.

Benefits:

  • Faster Query Performance: Significantly speeds up the retrieval of data.
  • Efficient Searching: Reduces the number of data pages to be read by the database engine.

Example:

CREATE INDEX idx_customer_name
ON Customers (CustomerName);

8. What are aggregate functions in SQL? Give examples.

Answer:
Aggregate functions perform calculations on multiple values and return a single value. They are often used with the GROUP BY clause in SQL statements.

Common Aggregate Functions:

  • COUNT(): Returns the number of rows that match the specified criteria.
  SELECT COUNT(*) FROM Orders;
  • SUM(): Returns the total sum of a numeric column.
  SELECT SUM(Quantity) FROM OrderDetails;
  • AVG(): Returns the average value of a numeric column.
  SELECT AVG(Price) FROM Products;
  • MIN(): Returns the minimum value of a column.
  SELECT MIN(Price) FROM Products;
  • MAX(): Returns the maximum value of a column.
  SELECT MAX(Price) FROM Products;

9. Explain the difference between WHERE and HAVING clauses.

Answer:

  • WHERE Clause:
  • Usage: Filters

rows before the data is grouped or aggregated. It is used to specify conditions on individual rows.

  • Scope: Can be used in SELECT, UPDATE, DELETE statements.
  • Example: SELECT * FROM Employees WHERE Department = 'Sales';
  • HAVING Clause:
  • Usage: Filters groups of rows after they are grouped or aggregated. It is used to specify conditions on aggregated data.
  • Scope: Typically used with the GROUP BY clause.
  • Example:
    sql SELECT Department, COUNT(*) FROM Employees GROUP BY Department HAVING COUNT(*) > 5;

10. What are subqueries in SQL? Explain with examples.

Answer:
A subquery is a query nested inside another query. Subqueries can be used in SELECT, INSERT, UPDATE, and DELETE statements, as well as in clauses like WHERE, FROM, and HAVING. There are different types of subqueries:

  • Single-row Subquery: Returns a single row.
  SELECT EmployeeName
  FROM Employees
  WHERE EmployeeID = (SELECT ManagerID FROM Departments WHERE DepartmentID = 1);
  • Multi-row Subquery: Returns multiple rows.
  SELECT EmployeeName
  FROM Employees
  WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE Location = 'New York');
  • Correlated Subquery: A subquery that references columns from the outer query. It is evaluated once for each row processed by the outer query.
  SELECT EmployeeName, Salary
  FROM Employees e
  WHERE Salary > (SELECT AVG(Salary) FROM Employees WHERE DepartmentID = e.DepartmentID);
  • Nested Subquery: A subquery inside another subquery.
  SELECT ProductName
  FROM Products
  WHERE ProductID IN (
      SELECT ProductID
      FROM OrderDetails
      WHERE OrderID IN (
          SELECT OrderID
          FROM Orders
          WHERE CustomerID = 1
      )
  );

Conclusion

Mastering SQL is essential for many roles in the data field. By understanding these top SQL interview questions and their detailed answers, you can demonstrate your proficiency and readiness to tackle real-world database challenges. Whether you are querying data, managing database structures, or ensuring data integrity, a strong grasp of SQL concepts and practices is invaluable.

Leave a Reply