0% found this document useful (0 votes)
6 views5 pages

SQL Queries 20questions

Uploaded by

KAIF Khan
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)
6 views5 pages

SQL Queries 20questions

Uploaded by

KAIF Khan
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/ 5

create table products

(
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');

create table employees


(
id int generated always as identity primary key,
name varchar(100)
);
insert into employees values(default,'Nina Kumari');
insert into employees values(default,'Abrar Khan');
insert into employees values(default,'Irene Costa');

create table sales_order


(
order_id int generated always as identity primary key,
order_date date,
quantity int,
prod_id int references products(id),
status varchar(20),
customer_id int references customers(id),
emp_id int,
constraint fk_so_emp foreign key (emp_id) references employees(id)
);
insert into sales_order
values(default,to_date('01-01-2024','dd-mm-yyyy'),2,1,'Completed',1,1);
insert into sales_order
values(default,to_date('01-01-2024','dd-mm-yyyy'),3,1,'Pending',2,2);
insert into sales_order
values(default,to_date('02-01-2024','dd-mm-yyyy'),3,2,'Completed',3,2);
insert into sales_order
values(default,to_date('03-01-2024','dd-mm-yyyy'),3,3,'Completed',3,2);
insert into sales_order
values(default,to_date('04-01-2024','dd-mm-yyyy'),1,1,'Completed',3,2);
insert into sales_order
values(default,to_date('04-01-2024','dd-mm-yyyy'),1,3,'completed',2,1);
insert into sales_order
values(default,to_date('04-01-2024','dd-mm-yyyy'),1,2,'On Hold',2,1);
insert into sales_order
values(default,to_date('05-01-2024','dd-mm-yyyy'),4,2,'Rejected',1,2);
insert into sales_order
values(default,to_date('06-01-2024','dd-mm-yyyy'),5,5,'Completed',1,2);
insert into sales_order
values(default,to_date('06-01-2024','dd-mm-yyyy'),1,1,'Cancelled',1,1);

SELECT * FROM products;


SELECT * FROM customers;
SELECT * FROM employees;
SELECT * FROM sales_order;

select sum(QUANTITY) AS Total_Products from sales_order ; -- 1. Identify the total


no of products sold

-- 2. Other than Completed, display the available delivery status's


select STATUS
from sales_order
where STATUS not in ('Completed', 'completed') ;

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 ;

-- 5. Display the total no of orders corresponding to each delivery status


select count(*) as total_orders,status
from sales_order
group by status
order by total_orders;

-- 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';

-- 7. Find the total number of orders corresponding to each delivery status by


ignoring the case in the delivery status.
-- The status with highest no of orders should be at the top.
select status ,count(*) as total_orders
from sales_order
group by status ;

select status ,
case when status = 'completed'
then 'Completed'
else status
end as updated_status
from sales_order ;

select updated_status ,count(*) as total_orders


from (select status ,
case when status = 'completed'
then 'Completed'
else status
end as updated_status
from sales_order) sq
group by updated_status
order by total_orders DESC;

select lower(status) as status , count(*) as Total_orders -- changes every status


into lower and then group by the lowercase status
from sales_order
group by lower(status)
order by total_orders desc ;

-- 8. Write a query to identify the total products purchased by each customer


select c.NAME as customer_name , sum(so.quantity) as total_products
from sales_order so
inner join customers c on c.ID = so.CUSTOMER_ID
group by c.NAME ;

-- 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.

select c.name , coalesce(sum(quantity) , 0) as total_products


from sales_order so
right join customers c on c.id = so.customer_id
group by c.name
order by total_products desc;

-- 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.

select e.name as employee , coalesce (sum( so.quantity * p.price) ,0)as total_sales

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.

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
order by 1,2; -- this is write w.r.t to both the tables

-- 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;

-- 18. Identify employees who have served more than 2 customers.

select e.name as employee_name , count( distinct c.name) as customer_served


from sales_order so
join customers c on c.id = so.customer_id
join employees e on e.id = so.emp_id
group by e.name
having count( distinct c.name) > 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;

SELECT * FROM products;


SELECT * FROM customers;
SELECT * FROM employees;
SELECT * FROM sales_order;

-- 20. Identify customers whose average purchase cost exceeds the average sale of
all the orders.

-- customers having average purchase cost


select c.name , avg(quantity * price ) as avg_purchase_cost
from sales_order so
join customers c on c.id = so.customer_id
join products p on p.id = so.prod_id
group by c.name
having avg(quantity * price ) > (select avg( p.price * quantity ) as average_sales
from sales_order so
join products p on p.id = so.prod_id) ;

You might also like