Skip to content

Commit 0b25cda

Browse files
Update sql_tips_ankit_bansal.md
1 parent 0f0c278 commit 0b25cda

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

‎sql_tips_ankit_bansal.md‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,125 @@ END;
331331
select * from orders;
332332

333333
```
334+
335+
---
336+
---
337+
338+
# 4 - SQL Self Join Concept | Most Asked Interview Question | Employee Salary More than Manager's Salary
339+
340+
341+
```sql
342+
343+
select * from sampletable;
344+
345+
select t1.emp_name , t1.salary from sampletable t1 JOIN
346+
sampletable t2 on t1.manager_id = t2.emp_id where t1.salary > t2.salary;
347+
348+
```
349+
350+
# 5 - How to Practice SQLs Without Creating Tables In Your Database
351+
352+
353+
```sql
354+
355+
356+
with emp1 as
357+
(
358+
select 1 as emp_id, 1000 as emp_salary, 1 as dep_id
359+
union all select 2 as emp_id, 2000 as emp_salary, 2 as dep_id
360+
union all select 3 as emp_id ,3000 as emp_salary, 3 as dep_id
361+
union all select 4 as emp_id ,4000 as emp_salary, 4 as dep_id
362+
),
363+
dep as
364+
(
365+
select 1 as dep_id ,'d1' as dep_name
366+
union all select 2 as dep_id, 'd1' as dep_name
367+
union all select 3 as dep_id, 'd2' as dep_name
368+
union all select 4 as dep_id, 'd3' as dep_name
369+
)
370+
select* from emp;
371+
372+
```
373+
---
374+
---
375+
376+
# 6 - SQL Cross Join | Use Cases | Master Data | Performance Data
377+
378+
379+
## Create Required Tables used in the video -
380+
381+
```sql
382+
383+
create table products (
384+
id int,
385+
name varchar(10)
386+
);
387+
insert into products VALUES
388+
(1, 'A'),
389+
(2, 'B'),
390+
(3, 'C'),
391+
(4, 'D'),
392+
(5, 'E');
393+
394+
395+
create table colors (
396+
color_id int,
397+
color varchar(50)
398+
);
399+
insert into colors values (1,'Blue'),(2,'Green'),(3,'Orange');
400+
401+
402+
create table sizes
403+
(
404+
size_id int,
405+
size varchar(10)
406+
);
407+
408+
insert into sizes values (1,'M'),(2,'L'),(3,'XL');
409+
410+
411+
create table transactions
412+
(
413+
order_id int,
414+
product_name varchar(20),
415+
color varchar(10),
416+
size varchar(10),
417+
amount int
418+
);
419+
insert into transactions values (1,'A','Blue','L',300),(2,'B','Blue','XL',150),(3,'B','Green','L',250),(4,'C','Blue','L',250),
420+
(5,'E','Green','L',270),(6,'D','Orange','L',200),(7,'D','Green','M',250);
421+
422+
```
423+
424+
* First use case is to produce master data for a fact table
425+
426+
```sql
427+
428+
429+
select * from transactions;
430+
431+
select product_name, color, size, sum(amount) as totalamount
432+
from transactions
433+
group by product_name, color, size;
434+
435+
with master_data as (select p.name as product_name , c.color , s.size from products p , colors c , sizes s)
436+
, sales as (select product_name, color, size , sum(amount) as totalamount
437+
from transactions
438+
group by product_name, color, size)
439+
select md.product_name, md.color, md.size , ifnull(s.totalamount,0) as totalamount from master_data md
440+
LEFT join sales s on md.product_name=s.product_name and md.color=s.color and md.size = s.size order by totalamount;
441+
442+
```
443+
444+
* Second use case is when you want to generate large no of records for performance testing.
445+
* Code shown below is just a example like how we can join table with large records to make a large dataset and manipulate calculations
446+
447+
448+
```sql
449+
450+
451+
select row_number() over (order by t.order_id) as order_id, t.product_name, t. color,
452+
case when row_number() over (order by t.order_id) %3=0 then 'L' else 'XL'end size
453+
,t.amount from transactions t;
454+
455+
```

0 commit comments

Comments
 (0)