-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path30_Subqueries.sql
More file actions
30 lines (25 loc) · 1.04 KB
/
30_Subqueries.sql
File metadata and controls
30 lines (25 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- Subquery is a query inside another query. Subqueries can be used in various places within a SQL statement, such as in the SELECT, FROM, WHERE, or HAVING clauses.
-- They allow you to perform operations that require multiple steps of data retrieval.
-- Example 1: Using a subquery in the WHERE clause
SELECT name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
)
-- This query retrieves the names and salaries of employees whose salary is greater than the average salary of all employees.
-- Example 2: Subquery with IN operator
SELECT name
FROM students
WHERE dept_id IN (
SELECT dept_id
FROM departments
WHERE dept_name = 'Computer Science'
)
-- This query retrieves the names of students who are in the 'Computer Science' department.
-- Example 3: Subquery in the SELECT clause
SELECT name,
salary,
(SELECT AVG(salary) FROM employees) AS avg_salary
FROM employees;
-- This query retrieves the names and salaries of employees along with the average salary of all employees as a separate column.