0% found this document useful (0 votes)
13 views6 pages

Subqueries Video SQL Scripts

SQL subqueries script

Uploaded by

bhai bhai
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
13 views6 pages

Subqueries Video SQL Scripts

SQL subqueries script

Uploaded by

bhai bhai
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 6

-- Create 'department' table

CREATE TABLE department


(
dept_id INT,
dept_name VARCHAR(50) PRIMARY KEY,
location VARCHAR(100)
);

-- Insert values into 'department' table


INSERT INTO department VALUES (1, 'Admin', 'Bangalore');
INSERT INTO department VALUES (2, 'HR', 'Bangalore');
INSERT INTO department VALUES (3, 'IT', 'Bangalore');
INSERT INTO department VALUES (4, 'Finance', 'Mumbai');
INSERT INTO department VALUES (5, 'Marketing', 'Bangalore');
INSERT INTO department VALUES (6, 'Sales', 'Mumbai');

-- Create 'employee' table


CREATE TABLE employee
(
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
dept_name VARCHAR(50) NOT NULL,
salary INT,
CONSTRAINT fk_emp FOREIGN KEY(dept_name) REFERENCES department(dept_name)
ON DELETE CASCADE ON UPDATE CASCADE
);

-- Insert values into 'employee' table


INSERT INTO employee VALUES(101, 'Mohan', 'Admin', 4000);
INSERT INTO employee VALUES(102, 'Rajkumar', 'HR', 3000);
INSERT INTO employee VALUES(103, 'Akbar', 'IT', 4000);
INSERT INTO employee VALUES(104, 'Dorvin', 'Finance', 6500);
INSERT INTO employee VALUES(105, 'Rohit', 'HR', 3000);
INSERT INTO employee VALUES(106, 'Rajesh', 'Finance', 5000);
INSERT INTO employee VALUES(107, 'Preet', 'HR', 7000);
INSERT INTO employee VALUES(108, 'Maryam', 'Admin', 4000);
INSERT INTO employee VALUES(109, 'Sanjay', 'IT', 6500);
INSERT INTO employee VALUES(110, 'Vasudha', 'IT', 7000);
INSERT INTO employee VALUES(111, 'Melinda', 'IT', 8000);
INSERT INTO employee VALUES(112, 'Komal', 'IT', 10000);
INSERT INTO employee VALUES(113, 'Gautham', 'Admin', 2000);
INSERT INTO employee VALUES(114, 'Manisha', 'HR', 3000);
INSERT INTO employee VALUES(115, 'Chandni', 'IT', 4500);
INSERT INTO employee VALUES(116, 'Satya', 'Finance', 6500);
INSERT INTO employee VALUES(117, 'Adarsh', 'HR', 3500);
INSERT INTO employee VALUES(118, 'Tejaswi', 'Finance', 5500);
INSERT INTO employee VALUES(119, 'Cory', 'HR', 8000);
INSERT INTO employee VALUES(120, 'Monica', 'Admin', 5000);
INSERT INTO employee VALUES(121, 'Rosalin', 'IT', 6000);
INSERT INTO employee VALUES(122, 'Ibrahim', 'IT', 8000);
INSERT INTO employee VALUES(123, 'Vikram', 'IT', 8000);
INSERT INTO employee VALUES(124, 'Dheeraj', 'IT', 11000);

-- Create 'employee_history' table


CREATE TABLE employee_history
(
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50) NOT NULL,
dept_name VARCHAR(50),
salary INT,
location VARCHAR(100),
CONSTRAINT fk_emp_hist_01 FOREIGN KEY(dept_name) REFERENCES
department(dept_name)
ON DELETE SET NULL ON UPDATE CASCADE,
CONSTRAINT fk_emp_hist_02 FOREIGN KEY(emp_id) REFERENCES employee(emp_id)
ON DELETE CASCADE ON UPDATE CASCADE
);

-- Create 'sales' table


CREATE TABLE sales
(
store_id INT,
store_name VARCHAR(50),
product_name VARCHAR(50),
quantity INT,
price INT
);

-- Insert values into 'sales' table


INSERT INTO sales VALUES
(1, 'Apple Store 1','iPhone 13 Pro', 1, 1000),
(1, 'Apple Store 1','MacBook pro 14', 3, 6000),
(1, 'Apple Store 1','AirPods Pro', 2, 500),
(2, 'Apple Store 2','iPhone 13 Pro', 2, 2000),
(3, 'Apple Store 3','iPhone 12 Pro', 1, 750),
(3, 'Apple Store 3','MacBook pro 14', 1, 2000),
(3, 'Apple Store 3','MacBook Air', 4, 4400),
(3, 'Apple Store 3','iPhone 13', 2, 1800),
(3, 'Apple Store 3','AirPods Pro', 3, 750),
(4, 'Apple Store 4','iPhone 12 Pro', 2, 1500),
(4, 'Apple Store 4','MacBook pro 16', 1, 3500);

select * from employee;


select * from department;
select * from employee_history;
select * from sales;

-- INTRO
--------------------------------------------------------------------------------
/* < WHAT IS SUBQUERIES? Sample subquery. How SQL processes this statement
containing subquery? > */

/* QUESTION: Find the employees who's salary is more than the average salary earned
by all employees. */
-- 1) find the avg salary
-- 2) filter employees based on the above avg salary
select *
from employee e
where salary > (select avg(salary) from employee)
order by e.salary;

-- TYPES OF SUBQUERY
--------------------------------------------------------------------------------
/* < SCALAR SUBQUERY > */
/* QUESTION: Find the employees who earn more than the average salary earned by all
employees. */
-- it return exactly 1 row and 1 column

select *
from employee e
where salary > (select avg(salary) from employee)
order by e.salary;

select e.*, round(avg_sal.sal,2) as avg_salary


from employee e
join (select avg(salary) sal from employee) avg_sal
on e.salary > avg_sal.sal;

--------------------------------------------------------------------------------
/* < MULTIPLE ROW SUBQUERY > */
-- Multiple column, multiple row subquery
/* QUESTION: Find the employees who earn the highest salary in each department. */
1) find the highest salary in each department.
2) filter the employees based on above result.
select *
from employee e
where (dept_name,salary) in (select dept_name, max(salary) from employee group by
dept_name)
order by dept_name, salary;

-- Single column, multiple row subquery


/* QUESTION: Find department who do not have any employees */
1) find the departments where employees are present.
2) from the department table filter out the above results.
select *
from department
where dept_name not in (select distinct dept_name from employee);

--------------------------------------------------------------------------------
/* < CORRELATED SUBQUERY >
-- A subquery which is related to the Outer query
/* QUESTION: Find the employees in each department who earn more than the average
salary in that department. */
1) find the avg salary per department
2) filter data from employee tables based on avg salary from above result.

select *
from employee e
where salary > (select avg(salary) from employee e2 where e2.dept_name=e.dept_name)
order by dept_name, salary;

/* QUESTION: Find department who do not have any employees */


-- Using correlated subquery
select *
from department d
where not exists (select 1 from employee e where e.dept_name = d.dept_name)

--------------------------------------------------------------------------------
/* < SUBQUERY inside SUBQUERY (NESTED Query/Subquery)> */
/* QUESTION: Find stores who's sales where better than the average sales accross
all stores */
1) find the sales for each store
2) average sales for all stores
3) compare 2 with 1
-- Using multiple subquery
select *
from (select store_name, sum(price) as total_sales
from sales
group by store_name) sales
join (select avg(total_sales) as avg_sales
from (select store_name, sum(price) as total_sales
from sales
group by store_name) x
) avg_sales
on sales.total_sales > avg_sales.avg_sales;

-- Using WITH clause


with sales as
(select store_name, sum(price) as total_sales
from sales
group by store_name)
select *
from sales
join (select avg(total_sales) as avg_sales from sales) avg_sales
on sales.total_sales > avg_sales.avg_sales;

-- CLAUSES WHERE SUBQUERY CAN BE USED


--------------------------------------------------------------------------------
/* < Using Subquery in WHERE clause > */
/* QUESTION: Find the employees who earn more than the average salary earned by
all employees. */
select *
from employee e
where salary > (select avg(salary) from employee)
order by e.salary;

--------------------------------------------------------------------------------
/* < Using Subquery in FROM clause > */
/* QUESTION: Find stores who's sales where better than the average sales accross
all stores */
-- Using WITH clause
with sales as
(select store_name, sum(price) as total_sales
from sales
group by store_name)
select *
from sales
join (select avg(total_sales) as avg_sales from sales) avg_sales
on sales.total_sales > avg_sales.avg_sales;

--------------------------------------------------------------------------------
/* < USING SUBQUERY IN SELECT CLAUSE > */
-- Only subqueries which return 1 row and 1 column is allowed (scalar or
correlated)
/* QUESTION: Fetch all employee details and add remarks to those employees who earn
more than the average pay. */
select e.*
, case when e.salary > (select avg(salary) from employee)
then 'Above average Salary'
else null
end remarks
from employee e;

-- Alternative approach
select e.*
, case when e.salary > avg_sal.sal
then 'Above average Salary'
else null
end remarks
from employee e
cross join (select avg(salary) sal from employee) avg_sal;

--------------------------------------------------------------------------------
/* < Using Subquery in HAVING clause > */
/* QUESTION: Find the stores who have sold more units than the average units sold
by all stores. */
select store_name, sum(quantity) Items_sold
from sales
group by store_name
having sum(quantity) > (select avg(quantity) from sales);

-- SQL COMMANDS WHICH ALLOW A SUBQUERY


--------------------------------------------------------------------------------
/* < Using Subquery with INSERT statement > */
/* QUESTION: Insert data to employee history table. Make sure not insert duplicate
records. */
insert into employee_history
select e.emp_id, e.emp_name, d.dept_name, e.salary, d.location
from employee e
join department d on d.dept_name = e.dept_name
where not exists (select 1
from employee_history eh
where eh.emp_id = e.emp_id);

--------------------------------------------------------------------------------
/* < Using Subquery with UPDATE statement > */
/* QUESTION: Give 10% increment to all employees in Bangalore location based on the
maximum
salary earned by an emp in each dept. Only consider employees in employee_history
table. */
update employee e
set salary = (select max(salary) + (max(salary) * 0.1)
from employee_history eh
where eh.dept_name = e.dept_name)
where dept_name in (select dept_name
from department
where location = 'Bangalore')
and e.emp_id in (select emp_id from employee_history);

--------------------------------------------------------------------------------
/* < Using Subquery with DELETE statement > */
/* QUESTION: Delete all departments who do not have any employees. */
delete from department d1
where dept_name in (select dept_name from department d2
where not exists (select 1 from employee e
where e.dept_name =
d2.dept_name));

You might also like