Skip to content

Commit a7ff27f

Browse files
committed
views, having clause, roll up, sql modes
1 parent 97e049a commit a7ff27f

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
-- FULL JOINED REVIEW QUERY (USED FREQUENTLY)
2+
SELECT title, released_year, genre, rating, first_name, last_name
3+
FROM reviews
4+
JOIN series ON series.id = reviews.series_id
5+
JOIN reviewers ON reviewers.id = reviews.reviewer_id;
6+
7+
-- CREATING A VIEW FOR THE ABOVE QUERY
8+
CREATE VIEW full_reviews AS
9+
SELECT title, released_year, genre, rating, first_name, last_name
10+
FROM reviews
11+
JOIN series ON series.id = reviews.series_id
12+
JOIN reviewers ON reviewers.id = reviews.reviewer_id;
13+
14+
-- USING THE VIEW LIKE A VIRTUAL TABLE
15+
SELECT * FROM full_reviews;
16+
17+
-- CREATING A VIEW TO ORDER SERIES BY YEAR (ASCENDING)
18+
CREATE VIEW ordered_series AS
19+
SELECT * FROM series
20+
ORDER BY released_year;
21+
22+
-- REPLACING THE VIEW TO ORDER BY YEAR (DESCENDING)
23+
CREATE OR REPLACE VIEW ordered_series AS
24+
SELECT * FROM series
25+
ORDER BY released_year DESC;
26+
27+
-- ALTERING EXISTING VIEW TO CHANGE SORT ORDER
28+
ALTER VIEW ordered_series AS
29+
SELECT * FROM series
30+
ORDER BY released_year;
31+
32+
-- DROPPING THE VIEW
33+
DROP VIEW ordered_series;
34+
35+
-- AGGREGATE: AVERAGE RATINGS FOR SERIES WITH MORE THAN 1 REVIEW
36+
SELECT
37+
title,
38+
AVG(rating) AS avg_rating,
39+
COUNT(rating) AS review_count
40+
FROM full_reviews
41+
GROUP BY title
42+
HAVING COUNT(rating) > 1;
43+
44+
-- AGGREGATE WITH ROLLUP: AVERAGE RATING PER TITLE
45+
SELECT
46+
title,
47+
AVG(rating) AS avg_rating
48+
FROM full_reviews
49+
GROUP BY title WITH ROLLUP;
50+
51+
-- AGGREGATE WITH ROLLUP: REVIEW COUNT PER TITLE
52+
SELECT
53+
title,
54+
COUNT(rating) AS review_count
55+
FROM full_reviews
56+
GROUP BY title WITH ROLLUP;
57+
58+
-- MULTI-COLUMN GROUPING WITH ROLLUP: AVG RATINGS PER REVIEWER/YEAR/GENRE
59+
SELECT
60+
first_name,
61+
released_year,
62+
genre,
63+
AVG(rating) AS avg_rating
64+
FROM full_reviews
65+
GROUP BY released_year, genre, first_name WITH ROLLUP;
66+
67+
-- To View Modes:
68+
SELECT @@GLOBAL.sql_mode;
69+
SELECT @@SESSION.sql_mode;
70+
71+
-- To Set Them:
72+
SET GLOBAL sql_mode = 'modes';
73+
SET SESSION sql_mode = 'modes';

0 commit comments

Comments
 (0)