Uploaded by Sourav Thakur

TASK 2 SQL (SOURAV THAKUR)

advertisement
NAME:-SOURAV THAKUR
CITY -> cityid, cityname
CUSTOMER -> custid, custname, dob, cityid, gender, custtype
1. Display all the customer names and their city names
2. Display city name wise no of customers
3. Display city name wise no of customers, no of customers who are male and no
of customers who are female
4. Display the youngest and the oldest customer name
5. Display the city name wise youngest customer name
6. Display the customer name who age is more than Mike and less than James
7. Display the customer names who are from the same type of Mike and their age
is less than Mike and are from the same city of Mike
8. Display the city names that do not have customers (write 2 types of queries for
this)
9. Display city names that have more customers than city Bangalore
10. Display city names that have the lowest no of customers
SOLUTIONS
Question 1:SELECT custname, cityname
FROM CUSTOMER
JOIN CITY ON CUSTOMER.cityid = CITY.cityid;
Question 2:SELECT cityname, COUNT(custid) AS
num_customers
FROM CUSTOMER
JOIN CITY ON CUSTOMER.cityid = CITY.cityid
GROUP BY cityname;
Question 3:SELECT CI.cityname,
COUNT(C.custid) AS no_of_customers,
SUM(CASE WHEN C.gender = 'Male' THEN 1
ELSE 0 END) AS no_of_males,
SUM(CASE WHEN C.gender = 'Female' THEN 1
ELSE 0 END) AS no_of_females
FROM CUSTOMER AS C
JOIN CITY AS CI ON C.cityid = CI.cityid
GROUP BY CI.cityname;
Question 4:SELECT custname
FROM CUSTOMER
ORDER BY dob ASC
LIMIT 1;
SELECT custname
FROM CUSTOMER
ORDER BY dob DESC
LIMIT 1;
Question 5
SELECT cityname,
custname
FROM CUSTOMER
JOIN CITY ON CUSTOMER.cityid = CITY.cityid
ORDER BY dob ASC
GROUP BY cityname;
Question 6
SELECT custname
FROM CUSTOMER
WHERE dob BETWEEN (SELECT dob FROM
CUSTOMER WHERE custname = 'Mike') AND (SELECT
dob FROM CUSTOMER WHERE custname = 'James');
Question 7
SELECT custname
FROM CUSTOMER
WHERE custtype = (SELECT custtype FROM
CUSTOMER WHERE custname = 'Mike') AND dob <
(SELECT dob FROM CUSTOMER WHERE custname =
'Mike') AND cityid = (SELECT cityid FROM
CUSTOMER WHERE custname = 'Mike');
Question 8
Type 1:SELECT cityname
FROM CITY
LEFT JOIN CUSTOMER ON CITY.cityid =
CUSTOMER.cityid
WHERE CUSTOMER.custid IS NULL;
Question 9
SELECT cityname
FROM CUSTOMER
JOIN CITY ON CUSTOMER.cityid = CITY.cityid
GROUP BY cityname
HAVING COUNT(*) > (SELECT COUNT(*) FROM
CUSTOMER WHERE cityname = 'Bangalore');
Question 10
SELECT cityname
FROM CUSTOMER
JOIN CITY ON CUSTOMER.cityid = CITY.cityid
GROUP BY cityname
ORDER BY COUNT(*) ASC
LIMIT 1;
Download