Skip to content
Open

SQL4 #57

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
30 changes: 30 additions & 0 deletions LeagueStatistics1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Write your MySQL query statement below
WITH CTE AS (
SELECT home_team_id as r1,
home_team_goals as goals_home,
away_team_goals as goals_away
FROM Matches
UNION ALL
SELECT away_team_id as r1,
away_team_goals as goals_home,
home_team_goals as goals_away
FROM Matches
)

SELECT
t.team_name,
COUNT(r1) AS matches_played,
SUM(CASE
WHEN c.goals_home > c.goals_away THEN 3
WHEN c.goals_home = c.goals_away THEN 1
ELSE 0
END) AS points,
SUM(c.goals_home) AS goal_for,
SUM(c.goals_away) AS goal_against,
SUM(c.goals_home) - SUM(c.goals_away) AS goal_diff
FROM Teams t
INNER JOIN
CTE c
ON t.team_id = c.r1
GROUP BY c.r1
ORDER BY points DESC, goal_diff DESC, t.team_name;
28 changes: 28 additions & 0 deletions SalesPerson.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Write your MySQL query statement below
#-----------------------Using CTE--------------------------------------------------------
WITH CTE AS(
SELECT o.sales_id
FROM
Orders o
LEFT JOIN
Company c
ON o.com_id = c.com_id
WHERE c.name = "RED"
)
SELECT s.name
FROM SalesPerson s
WHERE s.sales_id NOT IN (SELECT sales_id FROM CTE);
#-----------------Using Subquery--------------------------------------------------------
-- SELECT
-- s.name as 'name'
-- FROM
-- SalesPerson as s
-- WHERE s.sales_id NOT IN (
-- SELECT o.sales_id
-- FROM
-- Orders o
-- LEFT JOIN
-- Company c
-- ON o.com_id = c.com_id
-- WHERE c.name = "RED"
-- );
16 changes: 16 additions & 0 deletions SeniorsJuniorstoJointhecompany.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
WITH CTE AS (
SELECT employee_id, experience,
SUM(salary) OVER (PARTITION BY experience ORDER BY salary, employee_id) AS 'rsum'
FROM
candidates
)
SELECT
'Senior' AS experience, COUNT(employee_id) AS 'accepted_candidates'
FROM CTE
WHERE experience = 'Senior' AND rsum <= 70000;
UNION
SELECT 'Junior' as experience, COUNT(employee_id) AS 'accepted_candidates'
FROM CTE
WHERE experience = 'Junior' AND rsum <= (SELECT 70000 - IFNULL(MAX(rsum), 0) FROM CTE WHERE experience = 'Senior' AND rsum<=70000)


20 changes: 20 additions & 0 deletions friendrequest2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Write your MySQL query statement below
WITH CTE AS(SELECT accepter_id
FROM
RequestAccepted
UNION ALL
SELECT requester_id
FROM
RequestAccepted
),
counter_table AS (
SELECT accepter_id as id,
COUNT(accepter_id) as num
FROM CTE
GROUP BY accepter_id
)

SELECT id, num
FROM counter_table
ORDER BY num DESC
LIMIT 1;