Skip to content

Commit 26fd758

Browse files
committedMar 10, 2022
adding sql problem
1 parent 484a608 commit 26fd758

6 files changed

+246
-0
lines changed
 
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#create table productss (id int,name varchar(10));
2+
#insert into productss VALUES (1, 'A'),(2, 'B'),(3, 'C'),(4, 'D'),(5, 'E');
3+
#create table colors (color_id int,color varchar(50));
4+
#insert into colors values (1,'Blue'),(2,'Green'),(3,'Orange');
5+
#create table sizes(size_id int,size varchar(10));
6+
#insert into sizes values (1,'M'),(2,'L'),(3,'XL');
7+
#create table transactions(order_id int,product_name varchar(20),color varchar(10),size varchar(10),amount int);
8+
#insert into transactions values (1,'A','Blue','L',300),(2,'B','Blue','XL',150),(3,'B','Green','L',250),(4,'C','Blue','L',250),
9+
#(5,'E','Green','L',270),(6,'D','Orange','L',200),(7,'D','Green','M',250);
10+
select * from productss;
11+
select * from colors;
12+
select * from sizes;
13+
select * from transactions;
14+
#cross join
15+
select * from productss,colors;
16+
select p.*,c.* from productss p,colors c;
17+
18+
#use case of cross join
19+
#prepare master data
20+
select * from transactions;
21+
#give me the sales for each product size,color
22+
select product_name,color,size,sum(amount) as total_amount
23+
from transactions
24+
group by product_name,color,size;
25+
26+
#if not sold show zero
27+
with master_data_cte as (
28+
select p.name as product_name,c.color,s.size from productss p,colors c,sizes s)
29+
, sales_cte as (select product_name,color,size,sum(amount) as total_amount
30+
from transactions
31+
group by product_name,color,size)
32+
select md.product_name,md.color,md.size , isnull(s.total_amount,0) as total_amount from master_data_cte md
33+
left join
34+
sales_cte s on md.product_name = s.product_name
35+
and md.color = s.color and md.size = s.size
36+
order by total_amount;
37+
38+
#use case 2 : prepare large no . of rows for performance testing
39+
#unique order id
40+
select row_number() over(order by t.order_id) as order_id,
41+
t.Product_Name,t.color,t.size,
42+
Case when row_number() over(order by t.order_id)%3=0 then 'L' else 'XL' end size,t.amount
43+
from transactions t,superstore_orders o;
44+
45+
#more rows by joining again with transaction
46+
select row_number() over(order by t.order_id) as order_id,
47+
t.Product_Name,t.color,t.size,
48+
Case when row_number() over(order by t.order_id)%3=0 then 'L' else 'XL' end size,t.amount
49+
from transactions t,superstore_orders o,transactions t1;
50+
#table structure will be created
51+
select * into transactions_test from transactions where 1=2;
52+
select * from transactions_test;
53+
#now insert
54+
insert into transactions_test
55+
select row_number() over(order by t.order_id) as order_id,
56+
t.Product_Name,t.color,t.size,
57+
Case when row_number() over(order by t.order_id)%3=0 then 'L' else 'XL' end size,t.amount
58+
from transactions t,superstore_orders o,transactions t1;
59+

‎41.cs-Highest Energy Consumption.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Highest Energy Consumption
2+
#Find the date with the highest total energy consumption from the Meta/Facebook
3+
#data centers. Output the date along with the total energy consumption across all data centers.
4+
5+
WITH amazon_datacentre as
6+
(select * from fb_eu_energy
7+
UNION
8+
select * from fb_asia_energy
9+
UNION
10+
select * from fb_na_energy)
11+
select date,tot_consumption from (
12+
select date,tot_consumption,
13+
dense_rank() over(order by tot_consumption desc) rnk from
14+
(select date,sum(consumption) as tot_consumption from amazon_datacentre
15+
group by date) b1 )b2
16+
where b2.rnk=1
17+
order by date
18+
;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#Most Asked SQL JOIN based Interview Question | # of Records after 4 types of JOINs
2+
#CREATE TABLE t1(id1 int );
3+
#CREATE TABLE t2(id2 int );
4+
5+
#insert into t1 values (1);
6+
#insert into t1 values (1);
7+
8+
#insert into t2 values (1);
9+
#insert into t2 values (1);
10+
#insert into t2 values (1);
11+
#delete from t2;
12+
13+
select * from t1;
14+
select * from t2;
15+
#how join works on duplicate keys
16+
#inner join,it will go and join with all 3 records from right table 2*3-6
17+
select * from t1
18+
inner join t2 on t1.id1=t2.id2;
19+
#result on inner join+not matching records from left
20+
select * from t1
21+
left join t2 on t1.id1=t2.id2;
22+
23+
#right= non matching records from left,but here all are matching so same no of records like above
24+
#inner join+not matching records from right
25+
select * from t1
26+
right join t2 on t1.id1=t2.id2;
27+
28+
#matching records from oth table & non matching records from both table,but there is no unmatching record,so result will be same
29+
#ssms only
30+
select * from t1
31+
full outer join t2 on t1.id1 = t2.id2;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#Most Asked SQL JOIN based Interview Question | # of Records after 4 types of JOINs
2+
CREATE TABLE t3(id int );
3+
CREATE TABLE t4(id int );
4+
5+
insert into t3 values (1);
6+
insert into t3 values (1);
7+
insert into t3 values (2);
8+
9+
insert into t4 values (1);
10+
insert into t4 values (1);
11+
insert into t4 values (1);
12+
insert into t4 values (3);
13+
14+
#delete from t2;
15+
select * from t3;
16+
select * from t4;
17+
18+
select * from t3
19+
inner join t4 on t3.id=t4.id;
20+
#result on inner join+not matching records from left
21+
select * from t3
22+
left join t4 on t3.id=t4.id;
23+
24+
#right= non matching records from left
25+
#inner join+not matching records from right
26+
select * from t3
27+
right join t4 on t3.id=t4.id;
28+
29+
#matching records from oth table & non matching records from both table
30+
#ssms only
31+
select * from t3
32+
full outer join t4 on t3.id = t4.id;
33+
34+
#mysql full outer join
35+
SELECT * FROM t3
36+
LEFT JOIN t4 ON t3.id = t4.id
37+
UNION
38+
SELECT * FROM t3
39+
right JOIN t4 ON t3.id = t4.id
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#Most Asked SQL JOIN based Interview Question | # of Records after 4 types of JOINs
2+
#CREATE TABLE t5(id int );
3+
#CREATE TABLE t6(id int );
4+
5+
#insert into t5 values (1);
6+
#insert into t5 values (1);
7+
#insert into t5 values (2);
8+
#insert into t5 values (2);
9+
#insert into t5 values (4);
10+
11+
#insert into t6 values (1);
12+
#insert into t6 values (1);
13+
#insert into t6 values (1);
14+
#insert into t6 values (3);
15+
#insert into t6 values (2);
16+
#insert into t6 values (2);
17+
18+
#delete from t2;
19+
select * from t5;
20+
select * from t6;
21+
22+
select * from t5
23+
inner join t6 on t5.id=t6.id;
24+
#result on inner join+not matching records from left,6+ 2*1=(2) 2 records of 2
25+
26+
#all are matching in left table so sresult will be same as inner join
27+
select * from t5
28+
left join t6 on t5.id=t6.id;
29+
30+
#right= non matching records from left
31+
#inner join+not matching records from right
32+
#6+1*2 =2 records of 2+ for non matching 3 1 record will be there =9 records
33+
select * from t5
34+
right join t6 on t5.id=t6.id;
35+
36+
#matching records from oth table & non matching records from both table
37+
#ssms only
38+
# 3*2 +2 +1 + 2*2 + 4 is not matching so 1 record for this + 3 not matching = 12 records
39+
select * from t3
40+
full outer join t4 on t3.id = t4.id;
41+
42+
#mysql full outer join
43+
SELECT * FROM t3
44+
LEFT JOIN t4 ON t3.id = t4.id
45+
UNION
46+
SELECT * FROM t3
47+
right JOIN t4 ON t3.id = t4.id

‎45. AK- ALL JOINS with nulls.sql

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#Most Asked SQL JOIN based Interview Question | # of Records after 4 types of JOINs
2+
#CREATE TABLE t7(id int );
3+
#CREATE TABLE t8(id int );
4+
5+
#insert into t7 values (1);
6+
#insert into t7 values (1);
7+
#insert into t7 values (2);
8+
#insert into t7 values (2);
9+
#insert into t7 values (4);
10+
#insert into t7 values (null);
11+
12+
#insert into t8 values (1);
13+
#insert into t8 values (1);
14+
#insert into t8 values (1);
15+
#insert into t8 values (3);
16+
#insert into t8 values (2);
17+
#insert into t8 values (2);
18+
#insert into t8 values (null);
19+
20+
#delete from t2;
21+
select * from t7;
22+
select * from t8;
23+
24+
#null cant be compare with other null,so null also not joined with another null
25+
26+
select * from t7
27+
inner join t8 on t7.id=t8.id;
28+
#result on inner join+not matching records from left,6+ 2*1=(2) 2 records of 2
29+
30+
#all are matching in left table so sresult will be same as inner join
31+
# 2*3 + 4+ 1 not matching+1 not matching =12
32+
select * from t7
33+
right join t8 on t7.id=t8.id;
34+
35+
#right= non matching records from left
36+
#inner join+not matching records from right
37+
#2*3 +4+ 1 not m+1 not m
38+
select * from t7
39+
left join t8 on t7.id=t8.id;
40+
41+
#matching records from oth table & non matching records from both table
42+
#ssms only
43+
# 3*2 +2 +1 + 2*2 + 4 is not matching so 1 record for this + 3 not matching = 12 records
44+
select * from t7
45+
full outer join t8 on t7.id=t8.id;
46+
47+
#mysql full outer join
48+
SELECT * FROM t3
49+
LEFT JOIN t4 ON t3.id = t4.id
50+
UNION
51+
SELECT * FROM t3
52+
right JOIN t4 ON t3.id = t4.id

0 commit comments

Comments
 (0)