diff --git a/Friend Requests II.sql b/Friend Requests II.sql new file mode 100644 index 0000000..5c1f09c --- /dev/null +++ b/Friend Requests II.sql @@ -0,0 +1,9 @@ +WITH CTE AS( +SELECT requester_id AS 'id' FROM RequestAccepted +UNION ALL +SELECT accepter_id AS 'id' FROM RequestAccepted +), +ACTE AS( + SELECT id, COUNT(id) AS 'num' FROM CTE GROUP BY id +) +SELECT id, num FROM ACTE WHERE num = (SELECT MAX(num) FROM ACTE) \ No newline at end of file diff --git a/League Statistics.sql b/League Statistics.sql new file mode 100644 index 0000000..f484d57 --- /dev/null +++ b/League Statistics.sql @@ -0,0 +1,13 @@ +WITH CTE AS( + SELECT home_team_id AS 'r1', away_team_id AS 'r2', home_team_goals AS 'g1', away_team_goals AS 'g2' FROM Matches + UNION ALL + SELECT away_team_id AS 'r1', home_team_id AS 'r2', away_team_goals AS 'g1', home_team_goals AS 'g2' FROM Matches +) +#SELECT * FROM CTE; +SELECT team_name, COUNT(c.r1) AS 'matches_played', SUM( + CASE + WHEN c.g1 > c.g2 THEN 3 + WHEN c.g1 = c.g2 THEN 1 + ELSE 0 + END +) AS 'points', SUM(c.g1) AS 'goal_for', SUM(c.g2) AS 'goal_against', SUM(c.g1) - SUM(c.g2) 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 \ No newline at end of file diff --git a/Sales Person.sql b/Sales Person.sql new file mode 100644 index 0000000..3b56999 --- /dev/null +++ b/Sales Person.sql @@ -0,0 +1,3 @@ +SELECT name FROM SalesPerson s WHERE sales_id NOT IN( + SELECT o.sales_id FROM Orders o JOIN Company c ON o.com_id = c.com_id WHERE c.name = 'Red' +) \ No newline at end of file diff --git a/The Number of Seniors and Juniors to Join the Company.sql b/The Number of Seniors and Juniors to Join the Company.sql new file mode 100644 index 0000000..08ac56d --- /dev/null +++ b/The Number of Seniors and Juniors to Join the Company.sql @@ -0,0 +1,7 @@ +WITH CTE AS( +SELECT *, SUM(salary) OVER (PARTITION BY experience ORDER BY salary, employee_id) as 'rsum' FROM Candidates +) +# SELECT * FROM CTE +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) \ No newline at end of file