Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed SQL3 #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
7 changes: 7 additions & 0 deletions Consecutive Numbers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Write your MySQL query statement below
WITH CTE AS (SELECT *, LEAD(num, 1) OVER() AS next_1, LEAD(num, 2) OVER() AS next_2
FROM logs)
SELECT DISTINCT num AS ConsecutiveNums
FROM CTE
WHERE num = next_1
AND num = next_2;
12 changes: 12 additions & 0 deletions Dynamic Pivoting of a Table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE PROCEDURE PivotProducts()
BEGIN
# Write your MySQL query statement below.
SET SESSION GROUP_CONCAT_MAX_LEN = 1000000;
SET @sql = null;
SELECT GROUP_CONCAT(DISTINCT CONCAT('SUM(IF(store = "',store,'", price,null))', store))INTO @sql
FROM Products;
SET @sql=CONCAT('SELECT product_id, ', @sql, ' FROM Products GROUP BY 1');
PREPARE STATEMENT FROM @sql;
EXECUTE STATEMENT;
DEALLOCATE PREPARE STATEMENT;
END
14 changes: 14 additions & 0 deletions Number of Passengers in Each Bus.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Write your MySQL query statement below
WITH CTE AS (
SELECT p.passenger_id, min(b.arrival_time) AS arrival_time FROM Passengers p
INNER JOIN Buses b
ON p.arrival_time <= b.arrival_time
GROUP BY p.passenger_id
)

SELECT b.bus_id, COUNT(CTE.arrival_time) AS passengers_CNT
FROM Buses b
LEFT JOIN CTE
ON b.arrival_time = CTE.arrival_time
GROUP BY b.bus_id
ORDER BY b.bus_id;
5 changes: 5 additions & 0 deletions User Activity.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Write your MySQL query statement below
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;