SQL Queries 20questions
SQL Queries 20questions
(
id int generated always as identity primary key,
name varchar(100),
price float,
release_date date
);
insert into products
values(default,'iPhone 15', 800, to_date('22-08-2023','dd-mm-yyyy'));
insert into products
values(default,'Macbook Pro', 2100, to_date('12-10-2022','dd-mm-yyyy'));
insert into products
values(default,'Apple Watch 9', 550, to_date('04-09-2022','dd-mm-yyyy'));
insert into products
values(default,'iPad', 400, to_date('25-08-2020','dd-mm-yyyy'));
insert into products
values(default,'AirPods', 420, to_date('30-03-2024','dd-mm-yyyy'));
create table customers
(
id int generated always as identity primary key,
name varchar(100),
email varchar(30)
);
insert into customers values(default,'Meghan Harley', 'mharley@demo.com');
insert into customers values(default,'Rosa Chan', 'rchan@demo.com');
insert into customers values(default,'Logan Short', 'lshort@demo.com');
insert into customers values(default,'Zaria Duke', 'zduke@demo.com');
select STATUS
from sales_order
where STATUS in ('Completed ' , 'completed') ;
select STATUS
from sales_order
where lower(STATUS) != 'completed' ;
-- 3. Display the order id, order_date and product_name for all the completed
orders.
select ORDER_ID , ORDER_DATE , NAME
from sales_order so
inner join products p on p.ID = so.PROD_ID
where lower(so.status) = 'completed' ;
-- 4. Sort the above query to show the earliest orders at the top. Also, display
the customer who purchased these orders.
select ORDER_ID , ORDER_DATE , p.NAME as product_name ,c.NAME as customer_name
from sales_order so
inner join products p on p.ID = so.PROD_ID
inner join customers c on so.customer_id = c.id
where lower(so.status) = 'completed'
order by ORDER_DATE ;
-- 6. How many orders are still not completed for orders purchasing more than 1
item?
select count(status) as not_completed_orders
from sales_order so
where quantity > 1
and lower(status) <> 'completed';
select status ,
case when status = 'completed'
then 'Completed'
else status
end as updated_status
from sales_order ;
-- 9. Display the total sales and average sales done for each day.
select order_date ,sum (price*quantity) as total_sales, avg(price) as average_sales
from sales_order so
inner join products p on p.ID = so.customer_id
group by order_date
order by order_date ;
-- 10. Display the customer name, employee name, and total sale amount of all
orders which are either on hold or pending.
select c.name as customer_name , e.name as employee_name , sum(so.quantity *
p.price) as total_sales
from sales_order so
inner join customers c on c.id = so.customer_id
inner join employees e on e.id = so.emp_id
inner join products p on p.id = so.prod_id
where so.status in ('On Hold' , 'Pending')
group by c.name , e.name ;
-- 11. Fetch all the orders which were neither completed/pending or were handled by
the employee Abrar. Display employee name and all details of order.
select E.NAME AS employee , so.*
from sales_order so
join employees e on e.id = so.emp_id
where lower(status) not in ('completed', 'pending')
and lower(e.name) like '%abrar%' ;
-- 12. Fetch the orders which cost more than 2000 but did not include the MacBook
Pro. Print the total sale amount as well.
select distinct order_id , quantity , price , p.name , (price * quantity) as
total_sales
from sales_order so
join products p on p.id = so.prod_id
where (quantity*price) > 2000 and lower(p.name) not like 'macbook%' ;
-- 13. Identify the customers who have not purchased any product yet.
--Ist Method
select distinct c.*
from customers c
left join sales_order so on so.customer_id = c.id
where so.order_id is null;
--2nd Method
select *
from customers
where id not in (select distinct customer_id
from sales_order)
-- 14. Write a query to identify the total products purchased by each customer.
--Return all customers irrespective of whether they have made a purchase or not.
--Sort the result with the highest no of orders at the top.
-- 15. Corresponding to each employee, display the total sales they made of all the
completed orders.
--Display total sales as 0 if an employee made no sales yet.
from sales_order so
right join employees e on e.id = so.emp_id
products p on p.id = so.prod_id and lower(status) in ('completed') -- placing the
condition with the join will also let you consider the record of irenca costa as it
is not complted but he is an employee and that you need to show
group by e.name
order by total_sales desc ;
-- 16. Re-write the above query to display the total sales made by each employee
corresponding to each customer.
--If an employee has not served a customer yet then display "-" under the customer.
-- 17. Re-write the above query to display only those records where the total sales
are above 1000
select e.name as employee_name , c.name as customer_name ,coalesce
(sum( so.quantity * p.price) ,0)as total_sales
from sales_order so
join products p on p.id = so.prod_id
join customers c on c.id = so.customer_id and lower(status) in ('completed')
right join employees e on e.id = so.emp_id -- here right join will ensure that you
would get all the employees irrespective whethere they have made a sale or not
group by e.name , c.name
having coalesce (sum( so.quantity * p.price) ,0) >1000
order by 1,2;
-- 19. Identify the customers who have purchased more than 5 products
select c.name as customer_name , sum(quantity) as product_count
from sales_order so
join customers c on c.id = so.customer_id
group by c.name
having sum(quantity) > 5
order by 1;
-- 20. Identify customers whose average purchase cost exceeds the average sale of
all the orders.