Uploaded by Sourav Thakur

SQL TASK 1 SOURAV THAKUR

advertisement
NAME:-SOURAV Thakur
SQL Scenario
Sample Sales Model
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Display customer name and their city name
Display the city names who do not have any customers
Display the customers who made sales in the current year
Display the product names that were sold more than 100 times in the current month
Display the city name wise no of customers and the no of customers who are Males and number
of customers who are Females
Display the customers who have their birthday in the current month
Display the product names that are yet to be launched
Display the city name wise number of customers, total sales amount, no of sales and the margin
Display the customers who made more sales than the customer Mike in the current month
Display the city names that have the highest number of customers
Question 1:SELECT c.custname, ci.cityname
FROM customer c JOIN city ci ON c.cityid = ci.cityid;
Question 2:-
SELECT ci.cityname FROM city ci LEFT JOIN customer c ON ci.cityid = c.cityid
WHERE c.custid IS NULL;
Question 3:SELECT c.custname
FROM customer c
JOIN sales s ON c.custid = s.custid
WHERE YEAR(s.saledate) = 2023;
Question 4:SELECT p.prodname FROM prod p JOIN sales s ON p.prodid = s.prodid
WHERE MONTH(s.saledate) = 10 GROUP BY p.prodname HAVING SUM(s.qty) > 100;
Question 5:SELECT ci.cityname, COUNT(c.custid) AS total_customers,
SUM(CASE WHEN c.gender = 'Male' THEN 1 ELSE 0 END) AS male_customers,
SUM(CASE WHEN c.gender = 'Female' THEN 1 ELSE 0 END) AS female_customers
FROM city ci LEFT JOIN customer c ON ci.cityid = c.cityid
GROUP BY ci.cityname;
Question 6:SELECT custname FROM customer WHERE MONTH(dob) = 10;
Question7:SELECT prodname FROM prod WHERE launchdate > CURDATE();
Qustion 8:SELECT ci.cityname, COUNT(c.custid) AS total_customers,
SUM(s.amount) AS total_sales_amount, COUNT(s.saleid) AS total_sales,
SUM(s.margin) AS total_margin FROM city ci
LEFT JOIN customer c ON ci.cityid = c.cityid
LEFT JOIN sales s ON c.custid = s.custid GROUP BY ci.cityname;
Question 9:SELECT c.custname FROM customer c JOIN sales s ON c.custid = s.custid
WHERE MONTH(s.saledate) = 10 GROUP BY c.custname
HAVING SUM(s.amount) > (SELECT SUM(s.amount) FROM customer c2
JOIN sales s ON c2.custid = s.custid
WHERE c2.custname = 'Mike' AND MONTH(s.saledate) = 10);
Qusetion 10:SELECT ci.cityname FROM city ci JOIN customer c ON ci.cityid = c.cityid
GROUP BY ci.cityname HAVING COUNT(c.custid) = (SELECT MAX(customer_count)
FROM (SELECT COUNT(custid) AS customer_count
FROM customer
GROUP BY cityid) AS counts);
Download