Skip to content

Commit ab59d37

Browse files
committed
minor updates
1 parent 0b6013f commit ab59d37

File tree

3 files changed

+74
-95
lines changed

3 files changed

+74
-95
lines changed

README.md

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ where lat_n < 137.2345;
251251
```
252252
https://www.hackerrank.com/challenges/weather-observation-station-13/problem
253253

254-
***Note:*** Whenever problem asks for max/min, go with sorting (or) `max()/min()` functions. Whenever problem statement has "each" keyword then group by can be used
254+
**Note:** Whenever problem asks for max/min, go with sorting (or) `max()/min()` functions. Whenever problem statement has "each" keyword then group by can be used.
255255

256256
4. `power()` function to square the numbers and sqrt() to square root
257257
https://www.w3schools.com/sql/func_mysql_power.asp
@@ -261,7 +261,9 @@ https://www.w3schools.com/sql/func_mysql_power.asp
261261
select lat_n, row_number() over (order by lat_n desc) as rnum2 from station;
262262
```
263263
https://www.hackerrank.com/challenges/weather-observation-station-20/problem
264+
264265
https://leetcode.com/problems/second-highest-salary/
266+
265267
https://leetcode.com/problems/nth-highest-salary/
266268

267269
---------------------------------------------------------------------------------------------
@@ -275,10 +277,9 @@ https://www.hackerrank.com/challenges/the-report/problem
275277

276278
2. if condition within the select clause
277279
```
278-
select if(grade<8, NULL, name), grade, marks
279-
from students join grades
280-
where marks between min_mark and max_mark
281-
order by grade desc, name asc;
280+
select if(grade<8, null, name), grade, marks
281+
from students inner join grades on marks between min_mark and max_mark
282+
order by grade desc, name asc, marks asc;
282283
```
283284
https://www.hackerrank.com/challenges/the-report/problem
284285

@@ -346,9 +347,12 @@ https://leetcode.com/problems/consecutive-numbers/
346347

347348
Joining condition is different
348349
``` datediff(w1.recordDate,w2.recordDate) = 1```
350+
349351
https://leetcode.com/problems/rising-temperature/
350352

351-
**order of execution** <br>
353+
-------
354+
355+
### order of execution
352356
(1) from <br>
353357
(2) where <br>
354358
(3) group by <br>
@@ -373,3 +377,41 @@ Note: "query execution order" is called logical query processing, as opposed to
373377
Do not mistake logical query execution for physical query execution, they are two different things.
374378

375379
The easiest way to find what is executed first, just look at the **execution plan** - in Microsoft SQL Server you even have the option of looking at the data transfer between operators live.
380+
381+
---------
382+
### Date Handling in MySQL
383+
384+
SQL Server MONTH() Function
385+
https://www.w3schools.com/sql/func_sqlserver_month.asp
386+
387+
```
388+
select month('2017/08/25') as mm;
389+
```
390+
391+
SQL Server DATEDIFF() Function
392+
https://www.w3schools.com/sql/func_sqlserver_datediff.asp
393+
```
394+
SELECT DATEDIFF(year, '2017/08/25', '2011/08/25') AS DateDiff;
395+
```
396+
```
397+
+--------------+-------------------------------------------------------------------+
398+
| Parameter | Description |
399+
+--------------+-------------------------------------------------------------------+
400+
| interval | Required. The part to return. Can be one of the following values: |
401+
| | year, yyyy, yy = Year |
402+
| | quarter, qq, q = Quarter |
403+
| | month, mm, m = month |
404+
| | dayofyear = Day of the year |
405+
| | day, dy, y = Day |
406+
| | week, ww, wk = Week |
407+
| | weekday, dw, w = Weekday |
408+
| | hour, hh = hour |
409+
| | minute, mi, n = Minute |
410+
| | second, ss, s = Second |
411+
| | millisecond, ms = Millisecond |
412+
| date1, date2 | Required. The two dates to calculate the difference between |
413+
+--------------+-------------------------------------------------------------------+
414+
```
415+
416+
417+

hackerrank/04basic_join/p04the_report.sql

Lines changed: 10 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -19,95 +19,6 @@ descending order. If there is more than one student with the same grade (1-7) as
1919
order those particular students by their marks in ascending order.
2020
*/
2121

22-
--step1: join without common field
23-
select * from Students join Grades;
24-
25-
select * from Students inner join Grades;
26-
27-
/*
28-
Sample Output
29-
19 Samantha 87 10 90 100
30-
19 Samantha 87 9 80 89
31-
19 Samantha 87 8 70 79
32-
19 Samantha 87 7 60 69
33-
19 Samantha 87 6 50 59
34-
19 Samantha 87 5 40 49
35-
19 Samantha 87 4 30 39
36-
19 Samantha 87 3 20 29
37-
19 Samantha 87 2 10 19
38-
19 Samantha 87 1 0 9
39-
21 Julia 96 10 90 100
40-
21 Julia 96 9 80 89
41-
21 Julia 96 8 70 79
42-
21 Julia 96 7 60 69
43-
21 Julia 96 6 50 59
44-
21 Julia 96 5 40 49
45-
21 Julia 96 4 30 39
46-
21 Julia 96 3 20 29
47-
21 Julia 96 2 10 19
48-
21 Julia 96 1 0 9
49-
11 Britney 95 10 90 100
50-
11 Britney 95 9 80 89
51-
11 Britney 95 8 70 79
52-
11 Britney 95 7 60 69
53-
11 Britney 95 6 50 59
54-
11 Britney 95 5 40 49
55-
11 Britney 95 4 30 39
56-
11 Britney 95 3 20 29
57-
11 Britney 95 2 10 19
58-
11 Britney 95 1 0 9
59-
32 Kristeen 100 10 90 100
60-
32 Kristeen 100 9 80 89
61-
32 Kristeen 100 8 70 79
62-
32 Kristeen 100 7 60 69
63-
32 Kristeen 100 6 50 59
64-
32 Kristeen 100 5 40 49
65-
32 Kristeen 100 4 30 39
66-
32 Kristeen 100 3 20 29
67-
32 Kristeen 100 2 10 19
68-
32 Kristeen 100 1 0 9
69-
*/
70-
71-
-- step 2: do the comparison for deciding the grade
72-
select name, grade, marks
73-
from students join grades
74-
where marks between min_mark and max_mark;
75-
76-
select name, grade, marks
77-
from students inner join grades
78-
where marks between min_mark and max_mark;
79-
80-
/*
81-
Samantha 9 87
82-
Julia 10 96
83-
Britney 10 95
84-
Kristeen 10 100
85-
Dyana 6 55
86-
Jenny 7 66
87-
Christene 9 88
88-
Meera 3 24
89-
Priya 8 76
90-
Priyanka 8 77
91-
Paige 8 74
92-
Jane 7 64
93-
Belvet 8 78
94-
Scarlet 9 80
95-
Salma 9 81
96-
Amanda 4 34
97-
Heraldo 10 94
98-
Stuart 10 99
99-
Aamina 8 77
100-
Amina 9 89
101-
Vivek 9 84
102-
*/
103-
104-
--step 3: rest of the conditions
105-
select if(grade<8, NULL, name), grade, marks
106-
from students join grades
107-
where marks between min_mark and max_mark
108-
order by grade desc, name asc;
109-
110-
11122
select if(grade<8, NULL, name), grade, marks
11223
from students inner join grades
11324
where marks between min_mark and max_mark
@@ -135,4 +46,14 @@ NULL 7 66
13546
NULL 6 55
13647
NULL 4 34
13748
NULL 3 24
49+
*/
50+
51+
select if(grade<8, null, name), grade, marks
52+
from students inner join grades on marks between min_mark and max_mark
53+
order by grade desc, name asc, marks asc;
54+
55+
/*
56+
As null, particularly not a legit value, is a pointer pointing to some invalid address,
57+
woud not get compared which is why even using DESC after MARKS will get you same result i.e
58+
in ascending order.
13859
*/

leetcode/notes.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Easy
2+
3+
4+
Medium
5+
6+
7+
comparing list to another list using `in` operator
8+
```
9+
select dept.name as "Department", emp.name as "Employee", emp.salary as "Salary"
10+
from employee emp inner join department dept on emp.departmentId = dept.id
11+
where (emp.departmentId, emp.salary) in (select departmentId, max(salary)
12+
from employee
13+
group by departmentId);
14+
```
15+
https://leetcode.com/problems/department-highest-salary/
16+

0 commit comments

Comments
 (0)