Skip to content

Commit b4d4a4d

Browse files
committed
adding SQL PROBLEMS
1 parent bab06f3 commit b4d4a4d

10 files changed

+505
-0
lines changed

58. AK-finding missing quater.sql

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
CREATE TABLE STORES (
2+
Store varchar(10),
3+
Quarter varchar(10),
4+
Amount int);
5+
6+
INSERT INTO STORES (Store, Quarter, Amount)
7+
VALUES ('S1', 'Q1', 200),
8+
('S1', 'Q2', 300),
9+
('S1', 'Q4', 400),
10+
('S2', 'Q1', 500),
11+
('S2', 'Q3', 600),
12+
('S2', 'Q4', 700),
13+
('S3', 'Q1', 800),
14+
('S3', 'Q2', 750),
15+
('S3', 'Q3', 900);
16+
17+
select * from STORES;
18+
#Method 1 Aggregation : 1+2+3+4=10 ,whatevver be missing 10 - summationn of other quater
19+
select store,10-sum((right(quarter,1))) as q_no from STORES
20+
group by store;
21+
22+
select store,'Q'+ cast(10-sum(cast(right(quarter,1) as int)) as char(2)) as q_no from STORES
23+
group by store;
24+
#Method 2 Recursive CTE
25+
WITH ctq as(
26+
select distinct store, 1 as q_no from stores
27+
union all
28+
select store, q_no+1 as q_no from ctq
29+
where q_no < 4
30+
)
31+
select * from ctq
32+
order by stores;
33+
34+
WITH ctq as(
35+
select distinct store, 1 as q_no from stores
36+
union all
37+
select store, q_no+1 as q_no from ctq
38+
where q_no < 4
39+
)
40+
, q as (select store,'Q'+ cast(q_no as char(1)) as q_no from ctq)
41+
select q.* from q left join stores s
42+
on q.store = s.store and q.q_no = s.quarter
43+
where s.store is null;
44+
45+
46+
#Method 3 Cross Join
47+
WITH ctq as(
48+
select distinct s1.store,s2.quarter
49+
from stores s1, stores s2
50+
)
51+
select q.* from cte q left join stores s
52+
on q.store = s.store and q.quarter = s.quarter
53+
where s.store is null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
create table exams (student_id int, subject varchar(20), marks int);
2+
#delete from exams;
3+
insert into exams values (1,'Chemistry',91),(1,'Physics',91)
4+
,(2,'Chemistry',80),(2,'Physics',90)
5+
,(3,'Chemistry',80)
6+
,(4,'Chemistry',71),(4,'Physics',54);
7+
8+
#find students with same marks in physics & chemistry
9+
SELECT * FROM exams
10+
where subject in("Physics","Chemistry")
11+
group by student_id
12+
Having count(distinct subject)=2 and count(distinct marks)=1; #if marks are same distinct count will be 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#Sort by one country always at top and others in ascending order | Custom sorting
2+
create table counntry (
3+
country_id int,
4+
countrynames varchar(20)
5+
);
6+
7+
#drop table country;
8+
#insert into counntry values (1,"Afganistan"),(2,"Australia"),(3,"China"),(1,"France"),(1,"Germany"),(1,"India"),(1,"Italy");
9+
10+
select * from counntry
11+
ORDER BY
12+
(CASE WHEN countrynames="India" then 0
13+
WHEN countrynames="China" then 1 else 2 end),
14+
countrynames;

60. SQL Queries for experienced.sql

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
select * from empt;
2+
select * from dept;
3+
4+
#find duplicate records in a table
5+
SELECT emp_name,salary, COUNT(*) AS CNT
6+
FROM empt
7+
GROUP BY emp_name,salary
8+
HAVING COUNT(*)>1 ;
9+
#-- delete all the duplicate records in a table
10+
WITH cte AS (
11+
SELECT
12+
emp_name,salary,
13+
ROW_NUMBER() OVER (
14+
PARTITION BY
15+
emp_name,salary
16+
ORDER BY
17+
emp_name,salary
18+
) row_num
19+
FROM
20+
empt
21+
)
22+
DELETE FROM cte WHERE row_num > 1;
23+
24+
# find the manager name for the employee
25+
# --where empid and managerid are on the same table
26+
SELECT e.emp_id, e.emp_name, m.emp_name
27+
FROM empt e
28+
LEFT JOIN empt m
29+
on e.Manager_Id = m.Emp_Id;
30+
31+
# find the second highest salary
32+
Select max(Salary) as Salary
33+
FROM empt
34+
WHERE Salary <(Select max(Salary) from empt) ;
35+
-- 1. Inner Query - Get the highest salary
36+
-- 2. Outer Query - Get the highest salary excluding the highest salary
37+
-- gives the second highest salary
38+
39+
-- find the employee with the second highest salary
40+
SELECT * FROM empt where Salary in
41+
(SELECT max(Salary) as Salary
42+
FROM empt
43+
WHERE Salary < (Select max(Salary) FROM empt) );
44+
45+
-- 3rd and Nth highest salary
46+
SELECT MIN(Salary) FROM -- OUTER QUERY
47+
( SELECT DISTINCT TOP 3 Salary -- INNER QUERY
48+
FROM empt
49+
ORDER BY Salary DESC
50+
) AS O
51+
-- Here 3 can be changed to N ; can be applied for any number.
52+
-- 1. Inner Query - Get the highest 3 salaries
53+
-- 2. Outer Query - Get the minimum salary from those salaries
54+
55+
-- query to find maximum salary from each department
56+
SELECT Department_Id, MAX(Salary) as Salary
57+
FROM empt
58+
GROUP BY Department_Id
59+
60+
#--alternative for TOP clause in SQL
61+
SELECT TOP 3 * FROM empt
62+
#Alternative
63+
SET ROWCOUNT 3
64+
Select * from empt
65+
SET ROWCOUNT 0
66+
67+
-- showing single row from a table twice in the results
68+
SELECT dep_name FROM dept d WHERE d.dep_name='IT'
69+
UNION ALL
70+
SELECT dep_name FROM dept d WHERE d.dep_name='IT'
71+
72+
-- find departments that have less than 3 employees
73+
SELECT e.department_id,d.dep_name
74+
FROM empt e
75+
JOIN dept d on e.department_id = d.dep_id
76+
GROUP BY e.department_id,d.dep_name HAVING COUNT(Emp_Id) < 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#CREATE TABLE Transaction_Tbl_1
2+
(
3+
CustID int ,
4+
TranID int ,
5+
TranAmt float ,
6+
TranDate date
7+
) ;
8+
9+
#drop table Transaction_Tbl_1 ;
10+
11+
#INSERT into Transaction_Tbl_1 VALUES (1001, 20001, 10000, CAST('2020-04-25' AS Date));
12+
#INSERT into Transaction_Tbl_1 VALUES (1001, 20002, 15000, CAST('2020-04-25' AS Date));
13+
#INSERT into Transaction_Tbl_1 VALUES (1001, 20003, 80000, CAST('2020-04-25' AS Date));
14+
#INSERT into Transaction_Tbl_1 VALUES (1001, 20004, 20000, CAST('2020-04-25' AS Date));
15+
#INSERT into Transaction_Tbl_1 VALUES (1002, 30001, 7000, CAST('2020-04-25' AS Date));
16+
#INSERT into Transaction_Tbl_1 VALUES (1002, 30002, 15000, CAST('2020-04-25' AS Date));
17+
#INSERT into Transaction_Tbl_1 VALUES (1002, 30003, 22000, CAST('2020-04-25' AS Date));
18+
19+
select * from Transaction_Tbl_1;
20+
#Method 1: windows function
21+
select
22+
*,
23+
TranAmt/maximum_TranAmt
24+
from(
25+
select
26+
*,
27+
max(TranAmt) over(partition by CustID ) as maximum_TranAmt
28+
from
29+
Transaction_Tbl_1
30+
) t;
31+
32+
#Method 2: Subquery
33+
select t2.*,
34+
t1.max_amt,
35+
(t2.TranAmt/t1.max_amt) as ratio
36+
from
37+
Transaction_Tbl_1 t2
38+
inner join
39+
(select CustID,Max(TranAmt) as max_amt
40+
from
41+
Transaction_Tbl_1
42+
group by CustID ) t1
43+
on t1.CustID=t2.CustID;
44+
45+
#method 3
46+
with t1 as
47+
(
48+
select * from Transaction_Tbl_1
49+
),
50+
t2 as
51+
(
52+
Select CustID,max(TranAmt) As max_amt from Transaction_Tbl_1 group by CustID
53+
)
54+
select t1.*,
55+
t2.max_amt,
56+
(t1.TranAmt/t2.max_amt) as ratio
57+
from t1
58+
join t2
59+
on t1.CustID=t2.CustID;
60+
61+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#create table Contests ( contest_id INT, hacker_id INT, name VARCHAR(200) );
2+
3+
#insert into Contests (contest_id, hacker_id, name) values (66406, 17973, 'Rose'), (66556, 79153, 'Angela'), (94828, 80275, 'Frank');
4+
5+
#create table Colleges( college_id INT, contest_id INT );
6+
7+
#insert into Colleges (college_id, contest_id) values (11219, 66406), (32473, 66556), (56685, 94828);
8+
9+
#create table Challenges ( challenge_id INT, college_id INT );
10+
11+
#insert into Challenges (challenge_id, college_id) values (18765, 11219), (47127, 11219), (60292, 32473), (72974, 56685);
12+
13+
#create table View_Stats ( challenge_id INT, total_views INT, total_unique_views INT );
14+
15+
#insert into View_Stats (challenge_id, total_views, total_unique_views) values (47127, 26, 19), (47127, 15, 14), (18765, 43, 10), (18765, 72, 13), (75516, 35, 17), (60292, 11, 10), (72974, 41, 15), (75516, 75, 11);
16+
17+
#create table Submission_Stats ( challenge_id INT, total_submissions INT, total_accepted_submissions INT );
18+
19+
#insert into Submission_Stats (challenge_id, total_submissions, total_accepted_submissions) values (75516, 34, 12), (47127, 27, 10), (47127, 56, 18), (75516, 74, 12), (75516, 83, 8), (72974, 68, 24), (72974, 82, 14), (47127, 28, 11);
20+
21+
/* Question*
22+
John interviews many candidates from different colleges using coding challenges and contests.
23+
Write a query which displays the contest_id, hacker_id, name, sums of total_submissions,
24+
total_accepted_submissions, total_views, and total_unique_views for each contest sorted by contest_id.
25+
Exclude the contest from the result if all four sums are zero .
26+
*/
27+
select * from Contests;
28+
select * from Colleges;
29+
select * from Challenges;
30+
select * from View_Stats;
31+
select * from Submission_Stats;
32+
33+
select con.contest_id,
34+
con.hacker_id,
35+
con.name,
36+
sum(total_submissions), #stats table
37+
sum(total_accepted_submissions), #stats table
38+
sum(total_views), #from view_stats
39+
sum(total_unique_views)
40+
from contests con
41+
join colleges col on con.contest_id = col.contest_id
42+
join challenges cha on col.college_id = cha.college_id
43+
left join
44+
(select challenge_id, sum(total_views) as total_views, sum(total_unique_views) as total_unique_views
45+
from view_stats group by challenge_id) vs on cha.challenge_id = vs.challenge_id
46+
left join
47+
(select challenge_id, sum(total_submissions) as total_submissions, sum(total_accepted_submissions) as total_accepted_submissions
48+
from submission_stats group by challenge_id) ss on cha.challenge_id = ss.challenge_id
49+
group by con.contest_id, con.hacker_id, con.name;
50+
/*
51+
having sum(total_submissions)!=0 or
52+
sum(total_accepted_submissions)!=0 or
53+
sum(total_views)!=0 or
54+
sum(total_unique_views)!=0
55+
order by contest_id;
56+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
create table covid(city varchar(50),days date,cases int);
2+
/*
3+
#delete from covid;
4+
insert into covid values('DELHI','2022-01-01',100);
5+
insert into covid values('DELHI','2022-01-02',200);
6+
insert into covid values('DELHI','2022-01-03',300);
7+
insert into covid values('MUMBAI','2022-01-01',100);
8+
insert into covid values('MUMBAI','2022-01-02',100);
9+
insert into covid values('MUMBAI','2022-01-03',300);
10+
insert into covid values('CHENNAI','2022-01-01',100);
11+
insert into covid values('CHENNAI','2022-01-02',200);
12+
insert into covid values('CHENNAI','2022-01-03',150);
13+
insert into covid values('BANGALORE','2022-01-01',100);
14+
insert into covid values('BANGALORE','2022-01-02',300);
15+
insert into covid values('BANGALORE','2022-01-03',200);
16+
insert into covid values('BANGALORE','2022-01-04',400);
17+
*/
18+
select * from covid;
19+
#find cities where covid cases increasing continuously
20+
21+
select * ,
22+
rank() over(partition by city order by days asc) as rn_days
23+
,rank() over(partition by city order by cases asc) as rn_cases
24+
from covid
25+
order by city,cases;
26+
27+
with abcd as(
28+
select *
29+
,rank() over(partition by city order by days asc) - rank() over(partition by city order by cases asc) as diff
30+
from covid )
31+
select city from abcd
32+
group by city
33+
having count(distinct diff) =1 and max(diff)=0 ;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#create table company_users
2+
(
3+
company_id int,
4+
user_id int,
5+
language varchar(20)
6+
);
7+
8+
#insert into company_users values (1,1,'English')
9+
,(1,1,'German')
10+
,(1,2,'English')
11+
,(1,3,'German')
12+
,(1,3,'English')
13+
,(1,4,'English')
14+
,(2,5,'English')
15+
,(2,5,'German')
16+
,(2,5,'Spanish')
17+
,(2,6,'German')
18+
,(2,6,'Spanish')
19+
,(2,7,'English');
20+
21+
#find companines who have atleast 2 users who speaks both the language english & german
22+
select company_id,count(1) as num_of_users from
23+
(
24+
select company_id,user_id
25+
from company_users
26+
where language in('English','German')
27+
group by company_id,user_id
28+
having count(1)=2 ) a
29+
group by company_id
30+
having count(1)>=2
31+
;
32+
33+
with temp as (
34+
select *, row_number() over (partition by user_id) as rn
35+
from company_users
36+
where language in('English','German')
37+
)
38+
select company_id, count(user_id) as num_of_users
39+
from temp
40+
where rn > 1
41+
group by company_id
42+
having count(user_id) > 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
select * from happiness_index;
2+
3+
4+
5+
select * from
6+
happiness_index
7+
order by
8+
case when country='India' then 3
9+
when country='Pakistan' then 2
10+
when country='Sri Lanka' then 1
11+
else 0 end desc,Happiness_2021 desc

0 commit comments

Comments
 (0)