Skip to content

Commit 74f2982

Browse files
committed
sql challenge on joins
1 parent 36b71d4 commit 74f2982

2 files changed

Lines changed: 148 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
-- Select the current database
2+
SELECT DATABASE();
3+
4+
-- Show all databases
5+
SHOW DATABASES;
6+
7+
-- Switch to the 'challenges' database
8+
USE challenges;
9+
10+
-- Show all tables in the current database
11+
SHOW TABLES;
12+
13+
-- Select all data from the 'reviewers' table
14+
SELECT * FROM reviewers;
15+
16+
-- Select all data from the 'series' table
17+
SELECT * FROM series;
18+
19+
-- Select all data from the 'reviews' table
20+
SELECT * FROM reviews;
21+
22+
-- Join 'series' and 'reviews' to get series title and corresponding ratings
23+
SELECT s.title, r.rating
24+
FROM series s
25+
JOIN reviews r ON s.id = r.series_id;
26+
27+
-- Get average rating per series, ordered by lowest to highest average rating
28+
SELECT s.title, ROUND(AVG(r.rating), 2) AS avg_rating
29+
FROM series s
30+
JOIN reviews r ON s.id = r.series_id
31+
GROUP BY s.title
32+
ORDER BY avg_rating ASC;
33+
34+
-- Join reviewers with their reviews
35+
SELECT r.first_name, r.last_name, r2.rating
36+
FROM reviewers r
37+
INNER JOIN reviews r2 ON r.id = r2.reviewer_id;
38+
39+
-- Find series that have not received any reviews
40+
SELECT s.title AS unreviewed_series
41+
FROM series s
42+
LEFT JOIN reviews r ON s.id = r.series_id
43+
WHERE r.rating IS NULL;
44+
45+
-- Get average rating per genre, sorted by average
46+
SELECT s.genre, ROUND(AVG(r.rating), 2) AS avg_rating
47+
FROM series s
48+
JOIN reviews r ON s.id = r.series_id
49+
GROUP BY s.genre
50+
ORDER BY avg_rating;
51+
52+
-- Reviewer summary: total ratings, min, max, avg rating, and activity status
53+
SELECT
54+
r1.first_name,
55+
r1.last_name,
56+
COUNT(r2.rating) AS number_ratings,
57+
IFNULL(MIN(r2.rating), 0) AS min_rating,
58+
IFNULL(MAX(r2.rating), 0) AS max_rating,
59+
IFNULL(ROUND(AVG(r2.rating), 2), 0) AS avg_rating,
60+
CASE
61+
WHEN IFNULL(MIN(r2.rating), 0) = 0 THEN 'INACTIVE'
62+
ELSE 'ACTIVE'
63+
END AS status
64+
FROM reviewers r1
65+
LEFT JOIN reviews r2 ON r1.id = r2.reviewer_id
66+
GROUP BY r1.first_name, r1.last_name
67+
ORDER BY r1.first_name, r1.last_name ASC;
68+
69+
-- Get all reviews with series title and reviewer name
70+
SELECT
71+
s.title,
72+
r1.rating,
73+
CONCAT(r2.first_name, ' ', r2.last_name)
74+
FROM reviews r1
75+
INNER JOIN series s ON s.id = r1.series_id
76+
INNER JOIN reviewers r2 ON r1.reviewer_id = r2.id;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
select database();
2+
show databases;
3+
use challenges;
4+
5+
-- Create a database named 'challenges'
6+
CREATE TABLE reviewers (
7+
id INT PRIMARY KEY AUTO_INCREMENT,
8+
first_name VARCHAR(20) NOT NULL,
9+
last_name VARCHAR(20) NOT NULL
10+
);
11+
12+
-- Create a table named 'series' with columns for id, title, released_year, and genre
13+
CREATE TABLE series (
14+
id INT PRIMARY KEY AUTO_INCREMENT,
15+
title VARCHAR(50),
16+
released_year YEAR,
17+
genre VARCHAR(20)
18+
);
19+
20+
-- Create a table named 'reviews' with columns for id, rating, series_id, and reviewer_id
21+
CREATE TABLE reviews (
22+
id INT PRIMARY KEY AUTO_INCREMENT,
23+
rating DECIMAL(2 , 1),
24+
series_id INT,
25+
reviewer_id INT,
26+
FOREIGN KEY (series_id) REFERENCES series (id),
27+
FOREIGN KEY (reviewer_id) REFERENCES reviewers (id)
28+
);
29+
30+
show tables;
31+
32+
-- Insert sample data into the 'series' table
33+
INSERT INTO series (title, released_year, genre) VALUES
34+
('Archer', 2009, 'Animation'),
35+
('Arrested Development', 2003, 'Comedy'),
36+
("Bob's Burgers", 2011, 'Animation'),
37+
('Bojack Horseman', 2014, 'Animation'),
38+
("Breaking Bad", 2008, 'Drama'),
39+
('Curb Your Enthusiasm', 2000, 'Comedy'),
40+
("Fargo", 2014, 'Drama'),
41+
('Freaks and Geeks', 1999, 'Comedy'),
42+
('General Hospital', 1963, 'Drama'),
43+
('Halt and Catch Fire', 2014, 'Drama'),
44+
('Malcolm In The Middle', 2000, 'Comedy'),
45+
('Pushing Daisies', 2007, 'Comedy'),
46+
('Seinfeld', 1989, 'Comedy'),
47+
('Stranger Things', 2016, 'Drama');
48+
49+
50+
INSERT INTO reviewers (first_name, last_name) VALUES
51+
('Thomas', 'Stoneman'),
52+
('Wyatt', 'Skaggs'),
53+
('Kimbra', 'Masters'),
54+
('Domingo', 'Cortes'),
55+
('Colt', 'Steele'),
56+
('Pinkie', 'Petit'),
57+
('Marlon', 'Crafford');
58+
59+
60+
INSERT INTO reviews(series_id, reviewer_id, rating) VALUES
61+
(1,1,8.0),(1,2,7.5),(1,3,8.5),(1,4,7.7),(1,5,8.9),
62+
(2,1,8.1),(2,4,6.0),(2,3,8.0),(2,6,8.4),(2,5,9.9),
63+
(3,1,7.0),(3,6,7.5),(3,4,8.0),(3,3,7.1),(3,5,8.0),
64+
(4,1,7.5),(4,3,7.8),(4,4,8.3),(4,2,7.6),(4,5,8.5),
65+
(5,1,9.5),(5,3,9.0),(5,4,9.1),(5,2,9.3),(5,5,9.9),
66+
(6,2,6.5),(6,3,7.8),(6,4,8.8),(6,2,8.4),(6,5,9.1),
67+
(7,2,9.1),(7,5,9.7),
68+
(8,4,8.5),(8,2,7.8),(8,6,8.8),(8,5,9.3),
69+
(9,2,5.5),(9,3,6.8),(9,4,5.8),(9,6,4.3),(9,5,4.5),
70+
(10,5,9.9),
71+
(13,3,8.0),(13,4,7.2),
72+
(14,2,8.5),(14,3,8.9),(14,4,8.9);

0 commit comments

Comments
 (0)