Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Consecutive Numbers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT DISTINCT l3.num AS 'ConsecutiveNums' FROM Logs l1, Logs l2, Logs l3 WHERE
l1.id = l2.id - 1
AND
l2.id = l3.id - 1
AND
l1.num = l2.num
AND
l2.num = l3.num;
18 changes: 18 additions & 0 deletions Dynamic Pivoting of a Table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE PROCEDURE PivotProducts()
BEGIN
# I was found this problem extremely challenging and was unable to find a video of this problem on the S30.

SET group_concat_max_len = 5000;
SELECT GROUP_CONCAT(DISTINCT 'MAX(CASE WHEN store = \'',
store,
'\' THEN price ELSE NULL END) AS ',
store
ORDER BY store) INTO @sql
FROM Products;
SET @sql = CONCAT('SELECT product_id, ',
@sql,
' FROM Products GROUP BY product_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
5 changes: 5 additions & 0 deletions Number of Passengers in Each Bus.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
WITH CTE AS(
SELECT p.passenger_id, p.arrival_time, MIN(b.arrival_time) AS 'btime' FROM Passengers p INNER JOIN Buses b ON p.arrival_time <= b.arrival_time GROUP BY p.passenger_id
)
# SELECT * FROM CTE
SELECT b.bus_id, COUNT(c.btime) AS 'passengers_cnt' FROM Buses b LEFT JOIN CTE c ON b.arrival_time = c.btime GROUP BY b.bus_id ORDER BY b.bus_id
1 change: 1 addition & 0 deletions User Activity.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT activity_date AS 'day', COUNT(DISTINCT user_id) AS 'active_users' FROM Activity WHERE activity_date > "2019-06-27" AND activity_date <= "2019-07-27" GROUP BY activity_date