Skip to content

Commit de2acff

Browse files
committed
Add MS SQL solutions
1 parent b53ba0a commit de2acff

28 files changed

+398
-0
lines changed

0175-CombineTwoTables.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT p.FirstName, p.LastName, a.City, a.State
3+
FROM Person AS p
4+
LEFT JOIN Address AS a
5+
ON p.PersonId = a.PersonId

0511-GamePlayAnalysisI.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT DISTINCT
3+
player_id,
4+
MIN(event_date) OVER (PARTITION BY player_id) AS first_login
5+
FROM Activity

0577-EmployeeBonus.sql

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT e.name, b.bonus
3+
FROM Employee AS e
4+
LEFT JOIN Bonus AS b
5+
ON e.empId = b.empId
6+
WHERE b.bonus IS NULL OR b.bonus < 1000

0584-FindCustomerReferee.sql

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT name
3+
FROM customer
4+
WHERE referee_id != 2 OR referee_id IS NULL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT customer_number
3+
FROM orders
4+
GROUP BY customer_number
5+
HAVING COUNT(order_number) = (
6+
SELECT TOP 1 COUNT(order_number)
7+
FROM orders
8+
GROUP BY customer_number
9+
ORDER BY COUNT(order_number) DESC
10+
)

0595-BigCountries.sql

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT name, population, area
3+
FROM World
4+
WHERE population > 25000000 or area > 3000000

0610-TriangleJudgement.sql

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT
3+
*,
4+
CASE
5+
WHEN x + y > z AND y + z > x AND x + z > y THEN 'Yes'
6+
ELSE 'No'
7+
END AS triangle
8+
FROM triangle

0613-ShortestDistanceInALines.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT MIN(p1.x - p2.x) AS shortest
3+
FROM point AS p1
4+
INNER JOIN point AS p2
5+
ON p1.x > p2.x

0620-NotBoringMovies.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT *
3+
FROM cinema
4+
WHERE id % 2 = 1 AND description != 'boring'
5+
ORDER BY rating DESC

0627-SwapSalary.sql

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* Write your T-SQL query statement below */
2+
UPDATE salary
3+
SET sex = (
4+
CASE WHEN sex = 'f'
5+
THEN 'm'
6+
ELSE 'f'
7+
end
8+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT actor_id, director_id
3+
FROM ActorDirector
4+
GROUP BY actor_id, director_id
5+
HAVING COUNT(actor_id) >= 3;

1068-ProductSalesAnalysisI.sql

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT p.product_name, s.year, s.price
3+
FROM Sales AS s
4+
INNER JOIN Product AS p
5+
ON s.product_id = p.product_id

1069-ProductSalesAnalysisII.sql

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT s.product_id, SUM(s.quantity) AS total_quantity
3+
FROM Sales AS s
4+
GROUP BY s.product_id

1082-SalesAnalysisI.sql

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT seller_id
3+
FROM (
4+
SELECT
5+
seller_id,
6+
rank() OVER (
7+
ORDER BY SUM(price) DESC
8+
) AS rank
9+
FROM sales
10+
GROUP BY seller_id
11+
) AS b
12+
WHERE rank = 1

1148-ArticleViewsI.sql

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT DISTINCT author_id AS id
3+
FROM Views
4+
WHERE author_id = viewer_id

1173-ImmediateFoodDeliveryI.sql

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* Write your T-SQL query statement below */
2+
WITH cte AS (
3+
SELECT
4+
delivery_id,
5+
CASE
6+
WHEN order_date = customer_pref_delivery_date
7+
Then 1
8+
Else 0
9+
End
10+
AS delivery_status
11+
FROM Delivery
12+
)
13+
14+
SELECT
15+
ROUND(SUM(CAST(delivery_status AS FLOAT)) * 100 / Count(delivery_id), 2)
16+
AS immediate_percentage
17+
FROM cte

1179-ReformatDepartmentTable.sql

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT *
3+
FROM (
4+
SELECT revenue, month + '_Revenue' AS m, id
5+
FROM DEPARTMENT
6+
) AS t
7+
PIVOT (
8+
SUM(revenue)
9+
FOR m IN (
10+
[Jan_Revenue],
11+
[Feb_Revenue],
12+
[Mar_Revenue],
13+
[Apr_Revenue],
14+
[May_Revenue],
15+
[Jun_Revenue],
16+
[Jul_Revenue],
17+
[Aug_Revenue],
18+
[Sep_Revenue],
19+
[Oct_Revenue],
20+
[Nov_Revenue],
21+
[Dec_Revenue]
22+
)
23+
) AS pivot_table

1211-QueriesQualityAndPercentage.sql

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT
3+
query_name,
4+
ROUND(AVG(rating * 1.0 / position), 2) AS quality,
5+
ROUND(AVG(
6+
CASE WHEN rating < 3
7+
THEN 100.0
8+
ELSE 0.0
9+
END), 2) AS poor_query_percentage
10+
FROM Queries
11+
GROUP BY query_name

1241-NumberOfCommentsPerPost.sql

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT
3+
s1.sub_id AS post_id,
4+
ISNULL(COUNT(DISTINCT s2.sub_id), 0) AS number_of_comments
5+
FROM Submissions AS s1
6+
LEFT JOIN Submissions AS s2
7+
ON s1.sub_id = s2.parent_id
8+
WHERE s1.parent_id IS NULL
9+
GROUP BY s1.sub_id

1251-AverageSellingPrice.sql

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Write your T-SQL query statement below */
2+
WITH cte AS (
3+
SELECT p.product_id, u.units, p.price * u.units AS total
4+
FROM Prices AS p
5+
LEFT JOIN UnitsSold AS u
6+
ON p.product_id = u.product_id AND
7+
u.purchase_date BETWEEN p.start_date AND p.end_date
8+
)
9+
10+
SELECT
11+
product_id,
12+
Round((Sum(total) * 1.0 / Sum(units)), 2) AS average_price
13+
FROM cte
14+
GROUP BY product_id

1280-StudentsAndExaminations.sql

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT
3+
t2.*,
4+
COALESCE(t1.attended_exams, 0) AS attended_exams
5+
FROM (
6+
SELECT DISTINCT
7+
students.student_id,
8+
student_name,
9+
subject_name,
10+
COUNT(*) OVER (PARTITION BY students.student_id, subject_name) AS attended_exams
11+
FROM students
12+
LEFT JOIN examinations
13+
ON students.student_id = examinations.student_id
14+
) AS t1
15+
RIGHT JOIN (
16+
SELECT
17+
students.student_id,
18+
students.student_name,
19+
subjects.subject_name
20+
FROM students, subjects
21+
) AS t2
22+
ON t1.student_id = t2.student_id and t1.subject_name = t2.subject_name
23+
ORDER BY student_id, subject_name

1303-FindTheTeamSize.sql

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT
3+
a.employee_id, b.team_size
4+
FROM Employee AS a
5+
INNER JOIN (
6+
SELECT
7+
team_id,
8+
count(employee_id) AS team_size
9+
FROM Employee
10+
GROUP BY team_id
11+
) AS b
12+
ON a.team_id = b.team_id
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT
3+
p.product_name AS 'product_name',
4+
SUM(o.unit) AS 'unit'
5+
FROM Orders o
6+
LEFT OUTER JOIN Products p
7+
ON p.product_id = o.product_id
8+
WHERE month(o.order_date) = 2 AND year(o.order_date) = 2020
9+
GROUP BY p.product_name
10+
HAVING SUM(o.unit) >= 100
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT id, name
3+
FROM Students
4+
WHERE department_id NOT IN (
5+
SELECT id FROM Departments
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT u.unique_id, e.name
3+
FROM Employees AS e
4+
LEFT JOIN EmployeeUNI AS u
5+
ON e.id = u.id

1407-TopTravellers.sql

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* Write your T-SQL query statement below */
2+
SELECT u.name, COALESCE(r.travelled_distance, 0) AS travelled_distance
3+
FROM Users AS u
4+
LEFT JOIN (
5+
SELECT user_id, SUM(distance) AS travelled_distance
6+
FROM Rides
7+
GROUP BY user_id
8+
) AS r
9+
ON u.id = r.user_id
10+
ORDER BY COALESCE(r.travelled_distance, 0) DESC, u.name

1435-CreateASessionBarChart.sql

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Write your T-SQL query statement below */
2+
WITH cte AS
3+
(
4+
SELECT duration/60 AS mins
5+
FROM sessions
6+
)
7+
8+
SELECT
9+
'[0-5>' AS bin,
10+
SUM(CASE
11+
WHEN mins >= 0 AND mins < 5 THEN 1
12+
ELSE 0
13+
END) AS total
14+
FROM cte
15+
UNION ALL
16+
SELECT
17+
'[5-10>' AS bin,
18+
SUM(CASE
19+
WHEN mins >= 5 AND mins < 10 THEN 1
20+
ELSE 0
21+
END) AS total
22+
FROM cte
23+
UNION ALL
24+
SELECT
25+
'[10-15>' AS bin,
26+
SUM(CASE
27+
WHEN mins >= 10 AND mins < 15 THEN 1
28+
ELSE 0
29+
END) AS total
30+
FROM cte
31+
UNION ALL
32+
SELECT
33+
'15 or more' AS bin,
34+
SUM(CASE
35+
WHEN mins >= 15 THEN 1
36+
ELSE 0
37+
END) AS total
38+
FROM cte

0 commit comments

Comments
 (0)