Microsoft SQL Server Database Quiz Questions

Chapter Name:Chapter 7 - DQL (Data Query Language)
Lesson Content Link:Sub Queries
Current Quiz Count:45
Progress
0%
Q1
True / False

Subqueries can be used in SELECT, WHERE, and FROM clauses.

Q2
True / False

Subqueries can only return a single value.

Q3
True / False

The EXISTS operator is used to check if a subquery returns any rows.

Q4
True / False

Subqueries cannot be used in JOIN conditions.

Q5
True / False

A subquery in the FROM clause is known as a derived table.

Q6
True / False

Subqueries in the SELECT clause must return a single value.

Q7
True / False

Correlated subqueries are executed once for each row processed by the outer query.

Q8
True / False

In a subquery, you can use aggregate functions without a GROUP BY clause.

Q9
True / False

Subqueries in the WHERE clause can be used with any comparison operator.

Q10
True / False

Subqueries used in the FROM clause cannot have aliases.

Q11
True / False

Subqueries that return multiple rows can be used with the IN operator.

Q12
True / False

Using subqueries in the SELECT clause can improve query performance.

Q13
True / False

A scalar subquery returns a single value and can be used where a single value is expected.

Q14
True / False

Subqueries in the SELECT clause must be correlated with the outer query.

Q15
True / False

Subqueries can be used to perform complex data transformations.

Q16
Multiple Choice

Which of the following SQL snippets correctly uses a subquery to find employees with salaries higher than the average salary?

SQL Code
SELECT EmployeeName
FROM Employees
WHERE Salary > (
 SELECT AVG(Salary)
 FROM Employees
);
Q17
Multiple Choice

Which SQL snippet uses a subquery to find departments with employees earning above a specified threshold?

SQL Code
SELECT DepartmentName
FROM Departments
WHERE DepartmentID IN (
 SELECT DepartmentID
 FROM Employees
 WHERE Salary > 70000
);
Q18
Multiple Choice

Which SQL code snippet demonstrates a subquery used in the ORDER BY clause?

SQL Code
SELECT EmployeeName
FROM Employees
ORDER BY (
 SELECT AVG(Salary)
 FROM Employees
);
Q19
Multiple Choice

Which SQL snippet uses a subquery in the WHERE clause to filter employees by department average salary?

SQL Code
SELECT EmployeeName
FROM Employees e1
WHERE Salary > (
 SELECT AVG(Salary)
 FROM Employees e2
 WHERE e1.DepartmentID = e2.DepartmentID
);
Q20
Multiple Choice

Which SQL snippet correctly uses a subquery to find employees who earn more than the average salary of their department, considering the subquery is correlated?

SQL Code
SELECT EmployeeName
FROM Employees e1
WHERE Salary > (
 SELECT AVG(Salary)
 FROM Employees e2
 WHERE e1.DepartmentID = e2.DepartmentID
);
Q21
Multiple Choice

Which SQL snippet uses a subquery to list employees who have a higher salary than the average salary of employees in their department?

SQL Code
SELECT EmployeeName
FROM Employees e1
WHERE Salary > (
 SELECT AVG(Salary)
 FROM Employees e2
 WHERE e1.DepartmentID = e2.DepartmentID
);
Q22
Multiple Choice

Which SQL snippet demonstrates the use of a subquery to find employees in departments that have more than 10 employees?

SQL Code
SELECT EmployeeName
FROM Employees
WHERE DepartmentID IN (
 SELECT DepartmentID
 FROM Employees
 GROUP BY DepartmentID
 HAVING COUNT(*) > 10
);
Q23
Multiple Choice

Which SQL snippet uses a subquery to find employees who do not have the lowest salary in their department?

SQL Code
SELECT EmployeeName
FROM Employees e1
WHERE Salary > (
 SELECT MIN(Salary)
 FROM Employees e2
 WHERE e1.DepartmentID = e2.DepartmentID
);
Q24
Multiple Choice

Which SQL snippet uses a subquery in the WHERE clause to exclude departments without any employees earning more than $50,000?

SQL Code
SELECT DepartmentName
FROM Departments
WHERE DepartmentID NOT IN (
 SELECT DepartmentID
 FROM Employees
 WHERE Salary > 50000
);
Q25
Multiple Choice

Which SQL snippet uses a subquery to identify employees who have the highest salary within each department?

SQL Code
SELECT EmployeeName
FROM Employees e1
WHERE Salary = (
 SELECT MAX(Salary)
 FROM Employees e2
 WHERE e1.DepartmentID = e2.DepartmentID
);
Q26
Multiple Choice

Which SQL snippet correctly uses a subquery to find employees with a salary less than the average salary of employees in a specific department?

SQL Code
SELECT EmployeeName
FROM Employees e1
WHERE Salary < (
 SELECT AVG(Salary)
 FROM Employees e2
 WHERE e1.DepartmentID = e2.DepartmentID
);
Q27
Multiple Choice

Which SQL snippet demonstrates the use of a subquery to select employees whose salaries are greater than the average salary of all employees?

SQL Code
SELECT EmployeeName
FROM Employees
WHERE Salary > (
 SELECT AVG(Salary)
 FROM Employees
);
Q28
Multiple Choice

Which SQL snippet uses a subquery to find employees who work in departments with more than 5 employees?

SQL Code
SELECT EmployeeName
FROM Employees
WHERE DepartmentID IN (
 SELECT DepartmentID
 FROM Employees
 GROUP BY DepartmentID
 HAVING COUNT(*) > 5
);
Q29
Multiple Choice

Which SQL snippet demonstrates a subquery to list departments with a maximum salary greater than $60,000?

SQL Code
SELECT DepartmentName
FROM Departments
WHERE DepartmentID IN (
 SELECT DepartmentID
 FROM Employees
 GROUP BY DepartmentID
 HAVING MAX(Salary) > 60000
);
Q30
Multiple Choice

Which SQL snippet correctly uses a subquery to find employees with a salary less than the average salary of employees in their own department?

SQL Code
SELECT EmployeeName
FROM Employees e1
WHERE Salary < (
 SELECT AVG(Salary)
 FROM Employees e2
 WHERE e1.DepartmentID = e2.DepartmentID
);
Q31
Single Choice

Which SQL code snippet demonstrates a valid use of a subquery in the WHERE clause?

SQL Code
SELECT *
FROM Employees
WHERE DepartmentID = (
 SELECT DepartmentID
 FROM Departments
 WHERE DepartmentName = 'Sales'
);
Q32
Single Choice

Which of the following SQL snippets correctly demonstrates a scalar subquery?

SQL Code
SELECT EmployeeName
FROM Employees
WHERE Salary = (
 SELECT MAX(Salary)
 FROM Employees
);
Q33
Single Choice

Which SQL code snippet shows a subquery used as a derived table in the FROM clause?

SQL Code
SELECT DepartmentName, AVG(Salary)
FROM (
 SELECT DepartmentName, Salary
 FROM Employees
) AS EmployeeData
GROUP BY DepartmentName;
Q34
Single Choice

What does the following SQL snippet demonstrate about correlated subqueries?

SQL Code
SELECT EmployeeName
FROM Employees e
WHERE Salary > (
 SELECT AVG(Salary)
 FROM Employees
 WHERE DepartmentID = e.DepartmentID
);
Q35
Single Choice

Which SQL code snippet demonstrates the use of a subquery in the SELECT clause to calculate the average salary?

SQL Code
SELECT EmployeeName,
 (SELECT AVG(Salary)
 FROM Employees) AS AvgSalary
FROM Employees;
Q36
Single Choice

Which of the following is true about using subqueries with the EXISTS operator?

Q37
Single Choice

Which SQL snippet demonstrates using a subquery to filter records based on the results of another subquery?

SQL Code
SELECT EmployeeName
FROM Employees
WHERE DepartmentID IN (
 SELECT DepartmentID
 FROM Employees
 WHERE Salary > (
 SELECT AVG(Salary)
 FROM Employees
 )
);
Q38
Single Choice

Which SQL snippet correctly uses a subquery to find employees with salaries higher than the average salary of their department?

SQL Code
SELECT EmployeeName
FROM Employees e1
WHERE Salary > (
 SELECT AVG(Salary)
 FROM Employees e2
 WHERE e1.DepartmentID = e2.DepartmentID
);
Q39
Single Choice

Which SQL snippet uses a subquery to list departments with at least one employee earning more than $60,000?

SQL Code
SELECT DepartmentName
FROM Departments
WHERE DepartmentID IN (
 SELECT DepartmentID
 FROM Employees
 WHERE Salary > 60000
);
Q40
Single Choice

Which SQL code snippet demonstrates a subquery used in the HAVING clause?

SQL Code
SELECT DepartmentID, COUNT(*) AS NumEmployees
FROM Employees
GROUP BY DepartmentID
HAVING COUNT(*) > (
 SELECT AVG(EmployeeCount)
 FROM (
 SELECT DepartmentID, COUNT(*) AS EmployeeCount
 FROM Employees
 GROUP BY DepartmentID
 ) AS DeptCounts
);
Q41
Single Choice

Which SQL snippet uses a subquery to identify employees who do not belong to any department listed in a given subquery?

SQL Code
SELECT EmployeeName
FROM Employees
WHERE DepartmentID NOT IN (
 SELECT DepartmentID
 FROM Departments
 WHERE DepartmentName LIKE '%Sales%'
);
Q42
True / False

Subqueries in the WHERE clause can return multiple columns if used with certain operators.

Q43
True / False

Subqueries can be used with aggregate functions to perform calculations on grouped data.

Q44
Single Choice

Which SQL code snippet demonstrates using a subquery to filter records based on aggregated data?

SQL Code
SELECT DepartmentName
FROM Departments
WHERE DepartmentID IN (
 SELECT DepartmentID
 FROM Employees
 GROUP BY DepartmentID
 HAVING AVG(Salary) > 50000
);
Q45
Single Choice

Which SQL code snippet uses a subquery to find employees who have the same manager as a specific employee?

SQL Code
SELECT EmployeeName
FROM Employees e1
WHERE ManagerID = (
 SELECT ManagerID
 FROM Employees e2
 WHERE EmployeeName = 'John Doe'
);