0% found this document useful (0 votes)
11 views

SQL Interview Queries

Sql queries

Uploaded by

Kenil Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Interview Queries

Sql queries

Uploaded by

Kenil Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL Interview Queries

Set 1: Basic Select and Join

Question 1 (Easy):
You have two tables, Employees and Departments:

Employees Table:
| EmployeeID | EmployeeName | DepartmentID |
|------------|--------------|--------------|
|1 | John | 101 |
|2 | Sarah | 102 |
|3 | Mike | 101 |

Departments Table:
| DepartmentID | DepartmentName |
|--------------|----------------|
| 101 | HR |
| 102 | IT |

Write a query to list all employee names along with their department names.

Answer:
SELECT e.EmployeeName, d.DepartmentName
FROM Employees e
JOIN Departments d
ON e.DepartmentID = d.DepartmentID;

Question 2 (Moderate):
Using the same Employees and Departments tables, write a query to display the number of
employees in each department.

Answer:
SELECT d.DepartmentName, COUNT(e.EmployeeID) AS EmployeeCount
FROM Departments d
LEFT JOIN Employees e
ON d.DepartmentID = e.DepartmentID
GROUP BY d.DepartmentName;
Set 2: Aggregation and Filtering

Question 1 (Easy):
Consider a Sales table with the following structure:

| SaleID | ProductID | SaleAmount |


|--------|-----------|------------|
| 1 | 1001 | 200 |
| 2 | 1002 | 150 |
| 3 | 1001 | 300 |

Write a query to calculate the total sales amount.

Answer:
SELECT SUM(SaleAmount) AS TotalSales
FROM Sales;

Question 2 (Moderate):
Using the same Sales table, write a query to list all products that have sales greater than
250.

Answer:
SELECT ProductID, SUM(SaleAmount) AS TotalSales
FROM Sales
GROUP BY ProductID
HAVING SUM(SaleAmount) > 250;

Set 3: Subqueries and Joins

Question 1 (Easy):
You have two tables, Orders and Customers:

Orders Table:
| OrderID | CustomerID | OrderDate |
|---------|------------|-----------|
|1 | 201 | 2023-09-10|
|2 | 202 | 2023-09-12|

Customers Table:
| CustomerID | CustomerName |
|------------|--------------|
| 201 | Alice |
| 202 | Bob |
Write a query to list all orders along with the customer names.

Answer:
SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Orders o
JOIN Customers c
ON o.CustomerID = c.CustomerID;

Question 2 (Moderate):
Using the same Orders and Customers tables, write a query to list customers who have
placed more than one order.

Answer:
SELECT c.CustomerName, COUNT(o.OrderID) AS OrderCount
FROM Customers c
JOIN Orders o
ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerName
HAVING COUNT(o.OrderID) > 1;

You might also like