Skip to content

Commit b1048da

Browse files
committed
adding sql problems
1 parent 5967672 commit b1048da

13 files changed

+294
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#float remove leading & trailing zero,take column which have decimal with precion,remove by casting as float
2+
3+
select * from superstore_orders;
4+
select sales,cast(sales as float) as Amount_trailed from superstore_orders
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#How to increment salaries of employees who have 2 years with the organization | Datediff
2+
select * from employe;
3+
#EMPLOYEE INCREMENTED SALARY IF THEY HAVE COMPLETED 2 YEARS
4+
#SSMS ONLY
5+
select id, firstname,lastname,DepartmentName,Salary,HireDate,datediff(YEAR,HireDAate,'2020-12-31') from employe
6+
WHERE datediff(YEAR,HireDate,'2020-12-31');
7+
8+
select id, firstname,lastname,DepartmentName,Salary,HireDAate from employe
9+
WHERE datediff(HireDAate,"2022-12-31") > 2
10+
11+
#How to increment salaries of employees who have 2 years with the organization
12+
select id, firstname,lastname,DepartmentName,Salary,Salary * 1.15 as incrementedSalary,HireDate,datediff(YEAR,HireDAate,'2020-12-31') from employe
13+
WHERE datediff(YEAR,HireDate,'2020-12-31');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
select * from superstore_orders;
2+
#ssms
3+
select
4+
translate(order_id,'0123456789',' ') as Empname,
5+
trim(translate(order_id,translate(order_id,'0123456789',' '),space(len(translate(order_id,'0123456789',' '))))) as Empname,
6+
order_id from superstore_orders;
7+
8+
#mysql
9+
select
10+
trim(replace(order_id,'2020-20182019',' ')) as emp_name,
11+
trim(replace(order_id,replace(order_id,'2020-20182019',' '),space(length(replace(order_id,'2020-20182019',' '))))) as emp_id,
12+
order_id from superstore_orders;
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#ow to calculate Running Totals and Cumulative Sum ?
2+
#order by shoul be on on unique column otherwise it will not work properly,duplicate record can give problem
3+
select *
4+
,sum(salary) over(order by emp_id)
5+
from empm
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#How to calculate YTD and MTD totals | Window Functions
2+
#Month-To-Date MTD & YTD Year-To-Date
3+
select order_id,order_date,sales as Total_due from superstore_orders;
4+
5+
select order_id,order_date,sales as Total_due
6+
,sum(sales) over(partition by YEAR(order_date) order by order_ID) as YTD
7+
#PARTITON BY CLAUSE IS NOT MANDATORY ,CAN SKIP IT,ORDER BY ALWAYS MANDATORY
8+
#IF FOUND DUPLICATE ,IT JUST MADE CALCULATION ONCE
9+
from superstore_orders
10+
ORDER BY ORDER_ID;
11+
12+
#AGGREGATE SHOULD BE ON LAST RECORD IF DUPLICATE BY WINDOW FRAMING(RANGE),CAN OBSERVE FOR SAME ORDER ID YTD_FRAME HAS DIFFERENT VALUES
13+
#might be as per requirement: FINAL SUM ON THE LAST RECORD AS PER Requirement
14+
#help full in 1st/last order of the quater or year,
15+
select order_id,order_date,sales as Total_due
16+
,sum(sales) over(partition by YEAR(order_date) order by order_ID) as YTD
17+
,sum(sales) over(partition by YEAR(order_date) order by order_ID
18+
ROWS BETWEEN UNBOUNDED PRECEDING AND current row) as YTD_FRAME
19+
from superstore_orders ;
20+
21+
#when we did not add these pharase ,instead of using the rows,sql server using range
22+
#single agreegated records on same orderid or order date if similar
23+
select order_id,order_date,sales as Total_due
24+
,sum(sales) over(partition by YEAR(order_date) order by order_ID) as YTD
25+
,sum(sales) over(partition by YEAR(order_date) order by order_ID
26+
ROWS BETWEEN UNBOUNDED PRECEDING AND current row) as YTD_FRAME
27+
,sum(sales) over(partition by YEAR(order_date) order by order_ID
28+
RANGE BETWEEN UNBOUNDED PRECEDING AND current row) as YTD_range
29+
from superstore_orders ;
30+
#what range does is it identify distinct records besed on what you have defined for the ordering (order by) key
31+
#for every record which have same key,considered as part of same record,treated as single record,7 same value should be output or all the same record
32+
#if want to treat every row distinct need to define rows explicitly this(ROWS BETWEEN UNBOUNDED PRECEDING AND current row)
33+
34+
#NOW FOR MONTH TO DATE CALCULATION
35+
select order_id,order_date,sales as Total_due
36+
,sum(sales) over(partition by YEAR(order_date) order by order_ID
37+
ROWS BETWEEN UNBOUNDED PRECEDING AND current row) as YTD_FRAME
38+
,sum(sales) over(partition by YEAR(order_date),month(order_date) order by order_ID
39+
ROWS BETWEEN UNBOUNDED PRECEDING AND current row) as MTD_FRAME
40+
from superstore_orders ;
41+
42+
#LETS SAY WE WANT CAL SUM FOR LAST 3 ROWS OR ORDERS,remove unbounded
43+
#GO BACK BY 2 ROWS COZ want to calculate PREVIOUS 2 ROWS + CURRENT
44+
select order_id,order_date,sales as Total_due
45+
,sum(sales) over(partition by YEAR(order_date) order by order_ID
46+
ROWS BETWEEN UNBOUNDED PRECEDING AND current row) as YTD_FRAME
47+
,sum(sales) over(partition by YEAR(order_date) order by order_ID
48+
ROWS BETWEEN 2 PRECEDING AND current row) as MTD_3
49+
from superstore_orders ;
50+
#3rd row =457+45+289=792(1,2,3 row sum)
51+
#4t row = 45+289+166(sum of 2,3,4 row sum)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# How to calculate the First and Last Order | FIRST_VALUE | LAST_VALUE | Window Functions
2+
SELECT * from superstore_orders;
3+
4+
select order_id,order_date,sales as Total_due
5+
,first_value(ORDER_ID) over(partition by YEAR(order_date) order by order_id,order_date) AS first_order
6+
,first_value(ORDER_ID) over(partition by YEAR(order_date) order by order_id,order_date
7+
ROWS BETWEEN UNBOUNDED PRECEDING AND current row) as first_order_frm
8+
,last_value(ORDER_ID) over(partition by YEAR(order_date) order by order_id,order_date) AS last_order
9+
,last_value(ORDER_ID) over(partition by YEAR(order_date) order by order_id,order_date
10+
ROWS BETWEEN current row and UNBOUNDED following) as last_order_frm
11+
from superstore_orders ;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
select * from emp;
2+
#Find employees with salary less than Dept average but more than average of any other Dept | ANY
3+
select emp_id, emp_name, S.department_id, salary from emp AS EEMP
4+
inner join
5+
(select department_id ,avg(salary) as AvgSalary from emp
6+
group by department_id) as S
7+
ON EEMP.department_id = S.department_id
8+
and salary=S.AvgSalary
9+
where salary > ANY (select Avg(E.salary) from emp E group by department_id); #employeey slary less than overall
10+
#ANY COMPARE SAALRY WITH ANY OF THE SALRY IN DEPARTMENT COMING OUT OF 2 RECORDS
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#How to find employees with highest salary in a department
2+
select * from emp;
3+
#1)
4+
select emp_id,emp_name,e.department_id,salary from emp e
5+
inner join
6+
(select department_id,max(salary) as MaxSalary from emp
7+
group by department_id) as Maxsalemp
8+
on e.department_id = Maxsalemp.department_id
9+
and e.salary = Maxsalemp.MaxSalary;
10+
11+
#2) to improve performance,use sql analytics functions,greatest will be rank as 1,2nd will be 2,this will avoid self joins
12+
select emp_id,emp_name,department_id,salary from
13+
(select emp_id,emp_name,department_id,salary
14+
,rank() over(partition by department_id order by salary desc) as max_salary
15+
from emp ) as e
16+
where e.max_salary = 1;
17+
18+
19+
20+
21+
22+
23+
24+
#4) Employees having less than average salary of the department but greater than the average salary of any other department
25+
Select EmployeeKey, FirstName, LastName, Emp.DepartmentName, Salary from dbo.Employee Emp
26+
INNER JOIN
27+
(Select DepartmentName, Avg(Salary) as AvgSalary from dbo.EMployee
28+
Group BY DepartmentName ) AvgEmpSal
29+
ON Emp.DepartmentName = AvgEmpSal.DepartmentName
30+
AND Emp.Salary < AvgEMpSal.AvgSalary
31+
WHERE Emp.Salary > ANY (Select Avg(Salary) from dbo.EMployee Group By DepartmentName)
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#Find employees with salary greater than department average / and less than company average
2+
3+
select emp_id,emp_name,e.department_id,salary from emp e
4+
INNER JOIN
5+
(select department_id,avg(salary) as AvgSalary from emp
6+
group by department_id) AS AvgEmpSal
7+
on e.department_id = AvgEmpSal.department_id
8+
and e.salary > AvgEmpSal.AvgSalary;
9+
10+
#Find employees having more than average salary of the department but less than company(overall) average
11+
select emp_id,emp_name,e.department_id,salary from emp e
12+
INNER JOIN
13+
(select department_id,avg(salary) as AvgSalary from emp
14+
group by department_id) AS AvgEmpSal
15+
on e.department_id = AvgEmpSal.department_id
16+
and e.salary > AvgEmpSal.AvgSalary
17+
where e.salary < (select avg(salary) from emp);
18+
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#How to find employees with salary greater than their manager's salary
2+
select * from empm;
3+
select emp_id,emp_name,department_id,salary from empm e
4+
where salary > (select salary from empm where emp_id = e.manager_id);
5+
6+
#to fetch manager name
7+
select e.emp_id,e.emp_name,e.department_id,e.salary,Mgr.manager_name,mgr.manager_salary from empm e
8+
join
9+
(select manager_id ,emp_name as manager_name,salary as manager_salary from empm) as mgr
10+
on e.emp_id = mgr.manager_id
11+
and e.salary > mgr.manager_salary;

36.Check For Alphanumeric values.sql

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#contains only alphanumeric values,% 0 or more char,range in brackets,1 plahabet & 1 numeric value
2+
select distinct column_name from table_name
3+
where column_name is not null and column_name like '%[a-z0-9]'
4+
#^other than specified in range
5+
select distinct column_name from table_name
6+
where column_name is not null and column_name like '%[^a-z0-9]'
7+
8+
#contains only alphnumeric values
9+
select distinct column_name from table_name
10+
where column_name is not null and column_name not like '%[^a-z0-9]'
11+
12+
#which have spaces
13+
select distinct column_name from table_name
14+
where column_name is not null and column_name like '%[^a-z0-9 ]'
15+
16+
#which have hiphen
17+
select distinct column_name from table_name
18+
where column_name is not null and column_name like '%[^a-z0-9()-]'
19+
20+
#which have hiphen & forward slash
21+
select distinct column_name from table_name
22+
where column_name is not null and column_name like '%[^a-z0-9()/-]'

MOCK_DATA.csv

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
id,FirstName,LastName,DepartmentName,Salary,HireDAate
2+
1,Radcliffe,Bilney,Accounting,47074.94,2019-03-03
3+
2,Toiboid,Lindenman,Business Development,44205.91,2020-12-29
4+
3,Aile,Boyde,Business Development,30757.29,2020-12-20
5+
4,Hanny,Baughan,Support,40340.14,2021-04-04
6+
5,Stanford,Liversidge,Marketing,47755.78,2019-11-20
7+
6,Moe,Heinschke,Research and Development,39670.74,2021-12-11
8+
7,Purcell,Draisey,Sales,37551.55,2019-11-03
9+
8,Orelia,Affuso,Training,41209.37,2020-06-17
10+
9,Jeffrey,Tabourier,Services,24391.11,2021-09-18
11+
10,Fidel,Dallicoat,Product Management,24845.60,2018-01-05
12+
11,Kelli,Aireton,Research and Development,48529.88,2020-10-06
13+
12,Nerte,Freda,Research and Development,47933.14,2016-11-23
14+
13,Jamey,Wassung,Human Resources,27311.78,2018-12-24
15+
14,Charity,Collum,Business Development,33013.75,2016-12-27
16+
15,Sula,Farran,Sales,39778.87,2021-08-09
17+
16,Siobhan,Hessentaler,Research and Development,39925.02,2019-01-20
18+
17,Caitlin,Scullin,Support,47393.68,2017-11-11
19+
18,Calhoun,Bradbury,Business Development,45907.71,2017-04-10
20+
19,Darrel,Nunns,Legal,25318.83,2016-12-13
21+
20,Felicdad,Thwaites,Sales,36652.53,2016-08-23
22+
21,Noami,Alchin,Marketing,22196.36,2017-01-30
23+
22,Maje,Reedyhough,Legal,21276.52,2021-06-09
24+
23,Denice,Faudrie,Business Development,23537.83,2021-10-12
25+
24,Vincenty,Tutsell,Sales,43874.58,2020-11-20
26+
25,Brear,Antoney,Services,34747.49,2019-04-28
27+
26,Akim,MacAlpyne,Human Resources,24673.22,2020-11-10
28+
27,Christoper,Olenikov,Legal,20247.39,2019-06-25
29+
28,Christiano,Condict,Sales,37785.85,2020-01-31
30+
29,Dev,Cardo,Marketing,32949.07,2020-06-02
31+
30,Aili,Deinhardt,Legal,32605.48,2017-01-14
32+
31,Gay,McBrearty,Product Management,47274.09,2021-02-02
33+
32,Marcela,Gunney,Product Management,30242.14,2019-12-19
34+
33,Sal,Braid,Legal,30007.10,2021-08-23
35+
34,Adrien,Petrina,Legal,32995.99,2019-06-08
36+
35,Mae,Younger,Services,35586.92,2016-12-07
37+
36,Bank,Crossley,Research and Development,44621.95,2020-03-23
38+
37,Megan,MacKee,Sales,39862.33,2019-09-11
39+
38,Malva,Moysey,Research and Development,25809.73,2018-02-22
40+
39,Ham,Feveryear,Services,34127.76,2019-01-05
41+
40,Harley,Tonge,Business Development,21076.60,2021-02-09
42+
41,Lola,Bristow,Support,34783.76,2019-11-23
43+
42,Aeriell,Skough,Support,46385.37,2022-01-13
44+
43,Phoebe,Manterfield,Accounting,34082.49,2020-10-18
45+
44,Obadias,Naulls,Support,23008.45,2018-03-29
46+
45,Gregorius,Bielfeld,Research and Development,39973.11,2019-01-31
47+
46,Bertha,Royds,Services,36765.16,2017-08-27
48+
47,Karoly,Crowther,Support,20503.55,2017-08-14
49+
48,Almeria,Rosellini,Accounting,24376.45,2022-01-28
50+
49,Marinna,Abrahami,Accounting,38637.16,2019-12-06
51+
50,Georgena,Sherebrook,Legal,22145.39,2017-06-07

MOCK_DATA.csv.bak

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
id,FirstName,LastName,DepartmentName,Salary,HireDAate
2+
1,Radcliffe,Bilney,Accounting,$47074.94,2019-03-03
3+
2,Toiboid,Lindenman,Business Development,$44205.91,2020-12-29
4+
3,Aile,Boyde,Business Development,$30757.29,2020-12-20
5+
4,Hanny,Baughan,Support,$40340.14,2021-04-04
6+
5,Stanford,Liversidge,Marketing,$47755.78,2019-11-20
7+
6,Moe,Heinschke,Research and Development,$39670.74,2021-12-11
8+
7,Purcell,Draisey,Sales,$37551.55,2019-11-03
9+
8,Orelia,Affuso,Training,$41209.37,2020-06-17
10+
9,Jeffrey,Tabourier,Services,$24391.11,2021-09-18
11+
10,Fidel,Dallicoat,Product Management,$24845.60,2018-01-05
12+
11,Kelli,Aireton,Research and Development,$48529.88,2020-10-06
13+
12,Nerte,Freda,Research and Development,$47933.14,2016-11-23
14+
13,Jamey,Wassung,Human Resources,$27311.78,2018-12-24
15+
14,Charity,Collum,Business Development,$33013.75,2016-12-27
16+
15,Sula,Farran,Sales,$39778.87,2021-08-09
17+
16,Siobhan,Hessentaler,Research and Development,$39925.02,2019-01-20
18+
17,Caitlin,Scullin,Support,$47393.68,2017-11-11
19+
18,Calhoun,Bradbury,Business Development,$45907.71,2017-04-10
20+
19,Darrel,Nunns,Legal,$25318.83,2016-12-13
21+
20,Felicdad,Thwaites,Sales,$36652.53,2016-08-23
22+
21,Noami,Alchin,Marketing,$22196.36,2017-01-30
23+
22,Maje,Reedyhough,Legal,$21276.52,2021-06-09
24+
23,Denice,Faudrie,Business Development,$23537.83,2021-10-12
25+
24,Vincenty,Tutsell,Sales,$43874.58,2020-11-20
26+
25,Brear,Antoney,Services,$34747.49,2019-04-28
27+
26,Akim,MacAlpyne,Human Resources,$24673.22,2020-11-10
28+
27,Christoper,Olenikov,Legal,$20247.39,2019-06-25
29+
28,Christiano,Condict,Sales,$37785.85,2020-01-31
30+
29,Dev,Cardo,Marketing,$32949.07,2020-06-02
31+
30,Aili,Deinhardt,Legal,$32605.48,2017-01-14
32+
31,Gay,McBrearty,Product Management,$47274.09,2021-02-02
33+
32,Marcela,Gunney,Product Management,$30242.14,2019-12-19
34+
33,Sal,Braid,Legal,$30007.10,2021-08-23
35+
34,Adrien,Petrina,Legal,$32995.99,2019-06-08
36+
35,Mae,Younger,Services,$35586.92,2016-12-07
37+
36,Bank,Crossley,Research and Development,$44621.95,2020-03-23
38+
37,Megan,MacKee,Sales,$39862.33,2019-09-11
39+
38,Malva,Moysey,Research and Development,$25809.73,2018-02-22
40+
39,Ham,Feveryear,Services,$34127.76,2019-01-05
41+
40,Harley,Tonge,Business Development,$21076.60,2021-02-09
42+
41,Lola,Bristow,Support,$34783.76,2019-11-23
43+
42,Aeriell,Skough,Support,$46385.37,2022-01-13
44+
43,Phoebe,Manterfield,Accounting,$34082.49,2020-10-18
45+
44,Obadias,Naulls,Support,$23008.45,2018-03-29
46+
45,Gregorius,Bielfeld,Research and Development,$39973.11,2019-01-31
47+
46,Bertha,Royds,Services,$36765.16,2017-08-27
48+
47,Karoly,Crowther,Support,$20503.55,2017-08-14
49+
48,Almeria,Rosellini,Accounting,$24376.45,2022-01-28
50+
49,Marinna,Abrahami,Accounting,$38637.16,2019-12-06
51+
50,Georgena,Sherebrook,Legal,$22145.39,2017-06-07

0 commit comments

Comments
 (0)