SELECT
, INSERT
, UPDATE
, and DELETE
statements, as well as in clauses like WHERE
, FROM
, and HAVING
.
=
, >
, <
, >=
, <=
, and <>
.
Example: Find the employee with the highest salary
(SELECT MAX(salary) FROM employees)
returns the highest salary from the employees
table. The main query then uses this value to find the employee(s) with that salary.
IN
, ANY
, ALL
, and EXISTS
.
Example: Find employees who work in the same district as Anand
(SELECT district FROM employees WHERE name = 'Anand')
returns the district where Anand works. The main query then finds all employees who work in that district.
(SELECT AVG(salary) FROM employees e2 WHERE e2.district = e1.district)
calculates the average salary for each district. The main query then compares each employee’s salary to the average salary in their district.
employees
table with the following structure:
id | name | salary | district | state |
---|---|---|---|---|
1 | Anand | 50000 | Karaikal | Puducherry |
2 | Bala | 60000 | Coimbatore | Tamil Nadu |
3 | Kavitha | 55000 | Chennai | Tamil Nadu |
4 | Raj | 70000 | Cuddalore | Tamil Nadu |
5 | Kumar | 45000 | Madurai | Tamil Nadu |
6 | Ram | 80000 | Salem | Tamil Nadu |
7 | Karthik | 90000 | Trichy | Tamil Nadu |
8 | David | 40000 | Tirupati | Andhra Pradesh |
9 | Kannan | 75000 | Bangalore | Karnataka |
10 | Siva | 65000 | Kannur | Kerala |
11 | Ramesh | 85000 | Wayanad | Kerala |
12 | Suresh | 95000 | Palakkad | Kerala |
13 | Sathish | 50000 | Idukki | Kerala |
14 | Sujatha | 60000 | Karaikal | Puducherry |
name | salary |
---|---|
Suresh | 95000 |
(SELECT MAX(salary) FROM employees)
finds the highest salary in the employees
table, which is 95000
. The main query then retrieves the employee(s) with that salary.
name | district |
---|---|
Anand | Karaikal |
Sujatha | Karaikal |
(SELECT district FROM employees WHERE name = 'Anand')
returns Karaikal
, the district where Anand works. The main query then retrieves all employees who work in Karaikal
.
IN
name | district |
---|---|
Bala | Coimbatore |
Kavitha | Chennai |
Raj | Cuddalore |
Kumar | Madurai |
Ram | Salem |
Karthik | Trichy |
(SELECT district FROM employees WHERE state = 'Tamil Nadu')
returns all districts in Tamil Nadu. The main query then retrieves employees who work in those districts.
name | salary | district |
---|---|---|
Bala | 60000 | Coimbatore |
Kavitha | 55000 | Chennai |
Raj | 70000 | Cuddalore |
Ram | 80000 | Salem |
Karthik | 90000 | Trichy |
Kannan | 75000 | Bangalore |
Ramesh | 85000 | Wayanad |
Suresh | 95000 | Palakkad |
(SELECT AVG(salary) FROM employees e2 WHERE e2.district = e1.district)
calculates the average salary for each district. The main query compares each employee’s salary to the average salary in their district and retrieves those who earn more.
SELECT
name | salary | state | avg_state_salary |
---|---|---|---|
Anand | 50000 | Puducherry | 55000.00 |
Bala | 60000 | Tamil Nadu | 65000.00 |
Kavitha | 55000 | Tamil Nadu | 65000.00 |
Raj | 70000 | Tamil Nadu | 65000.00 |
Kumar | 45000 | Tamil Nadu | 65000.00 |
Ram | 80000 | Tamil Nadu | 65000.00 |
Karthik | 90000 | Tamil Nadu | 65000.00 |
David | 40000 | Andhra Pradesh | 40000.00 |
Kannan | 75000 | Karnataka | 75000.00 |
Siva | 65000 | Kerala | 76250.00 |
Ramesh | 85000 | Kerala | 76250.00 |
Suresh | 95000 | Kerala | 76250.00 |
Sathish | 50000 | Kerala | 76250.00 |
Sujatha | 60000 | Puducherry | 55000.00 |
(SELECT AVG(salary) FROM employees e2 WHERE e2.state = e1.state)
calculates the average salary for each state. This value is displayed alongside each employee’s details.
HAVING
district | avg_district_salary |
---|---|
Bangalore | 75000.00 |
Palakkad | 95000.00 |
Salem | 80000.00 |
Trichy | 90000.00 |
Wayanad | 85000.00 |
(SELECT AVG(salary) FROM employees)
calculates the overall average salary. The main query groups employees by district and retrieves districts where the average salary is greater than the overall average.
FROM
FROM
clause is called as Derived table in SQL Server.
Find the top 3 highest-paid employees
name | salary |
---|---|
Suresh | 95000 |
Karthik | 90000 |
Ramesh | 85000 |
(SELECT name, salary FROM employees ORDER BY salary DESC)
retrieves all employees sorted by salary in descending order. The main query then limits the result to the top 3 rows.
SELECT
, INSERT
, UPDATE
, and DELETE
statements.