0% found this document useful (0 votes)
6 views4 pages

Join Queries Answer Key

The document contains SQL commands for creating and querying two databases: one for students and their papers, and another for series and reviews. It includes various SELECT statements to retrieve data, calculate averages, and determine statuses based on ratings. The queries demonstrate the use of JOINs, GROUP BY, and conditional logic to manipulate and present the data effectively.

Uploaded by

navedp21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Join Queries Answer Key

The document contains SQL commands for creating and querying two databases: one for students and their papers, and another for series and reviews. It includes various SELECT statements to retrieve data, calculate averages, and determine statuses based on ratings. The queries demonstrate the use of JOINs, GROUP BY, and conditional logic to manipulate and present the data effectively.

Uploaded by

navedp21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

CREATE TABLE students (

id INT PRIMARY KEY AUTO_INCREMENT,


first_name VARCHAR(50)
);

CREATE TABLE papers (


title VARCHAR(50),
grade INT,
student_id INT,
FOREIGN KEY (student_id)
REFERENCES students (id)
);

Q1.
SELECT
first_name, title, grade
FROM
students
JOIN
papers ON papers.student_id = students.id
ORDER BY grade DESC;

Q2.
SELECT
first_name, title, grade
FROM
students
LEFT JOIN
papers ON papers.student_id = students.id;

Q3.
SELECT
first_name, IFNULL(title, 'MISSING'), IFNULL(grade, 0)
FROM
students
LEFT JOIN
papers ON papers.student_id = students.id;

Q4.
SELECT
first_name, IFNULL(AVG(grade), 0) AS average
FROM
students
LEFT JOIN
papers ON students.id = papers.student_id
GROUP BY first_name
ORDER BY average DESC;

Q5.
SELECT
first_name,
IFNULL(AVG(grade), 0) AS average,
CASE
WHEN IFNULL(AVG(grade), 0) >= 75 THEN 'passing'
ELSE 'failing'
END AS passing_status
FROM
students
LEFT JOIN
papers ON students.id = papers.student_id
GROUP BY first_name
ORDER BY average DESC;

-----------------------------------------------------------------------

Q1.
SELECT
title, rating
FROM
series
JOIN
reviews ON series.id = reviews.series_id

Q2.
SELECT
title, ROUND(AVG(rating), 2) AS avg_rating
FROM
series
JOIN
reviews ON series.id = reviews.series_id
GROUP BY title
ORDER BY avg_rating

Q3.
SELECT
first_name, last_name, rating
FROM
reviewers
JOIN
reviews ON reviews.reviewer_id = reviewers.id;

Q4.
SELECT
title AS unreviewed_series
FROM
series
LEFT JOIN
reviews ON series.id = reviews.series_id
WHERE
rating IS NULL;

SELECT
title AS unreviewed_series
FROM
reviews
RIGHT JOIN
series ON series.id = reviews.series_id
WHERE
rating IS NULL;
Q5.
SELECT
genre, ROUND(AVG(rating), 2) AS avg_rating
FROM
series
JOIN
reviews ON series.id = reviews.series_id
GROUP BY genre;

Q6.
-- USING CASE
SELECT
first_name,
last_name,
COUNT(rating) AS count,
IFNULL(MIN(rating), 0) AS min,
IFNULL(MAX(rating), 0) AS max,
ROUND(IFNULL(AVG(rating), 0), 2) AS average,
CASE
WHEN COUNT(rating) >= 10 THEN 'POWERUSER'
WHEN COUNT(rating) > 0 THEN 'ACTIVE'
ELSE 'INACTIVE'
END AS status
FROM
reviewers
LEFT JOIN
reviews ON reviewers.id = reviews.reviewer_id
GROUP BY first_name , last_name;

-- USING IF
SELECT
first_name,
last_name,
COUNT(rating) AS count,
IFNULL(MIN(rating), 0) AS min,
IFNULL(MAX(rating), 0) AS max,
ROUND(IFNULL(AVG(rating), 0), 2) AS average,
IF(COUNT(rating) > 0,
'ACTIVE',
'INACTIVE') AS status
FROM
reviewers
LEFT JOIN
reviews ON reviewers.id = reviews.reviewer_id
GROUP BY first_name , last_name;

Q7.
SELECT
title,
rating,
CONCAT(first_name, ' ', last_name) AS reviewer
FROM
reviews
INNER JOIN
series ON reviews.series_id = series.id
INNER JOIN
reviewers ON reviews.reviewer_id = reviewers.id;
SELECT
title,
rating,
CONCAT(first_name, ' ', last_name) AS reviewer
FROM
series
INNER JOIN
reviews ON reviews.series_id = series.id
INNER JOIN
reviewers ON reviews.reviewer_id = reviewers.id;

SELECT
title,
rating,
CONCAT(first_name, ' ', last_name) AS reviewer
FROM
reviewers
INNER JOIN
reviews ON reviews.reviewer_id = reviewers.id
INNER JOIN
series ON reviews.series_id = series.id

You might also like