0% found this document useful (0 votes)
3 views

SQL Lab-7

The document contains a series of SQL queries executed on a MariaDB database named 'lab7', which includes tables for movies, ratings, and reviewers. The queries demonstrate various operations such as selecting data, filtering results, and grouping by attributes to analyze movie ratings and reviewer interactions. Key findings include the identification of movies not rated by specific reviewers and the count of ratings per movie.

Uploaded by

21ucs037
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)
3 views

SQL Lab-7

The document contains a series of SQL queries executed on a MariaDB database named 'lab7', which includes tables for movies, ratings, and reviewers. The queries demonstrate various operations such as selecting data, filtering results, and grouping by attributes to analyze movie ratings and reviewer interactions. Key findings include the identification of movies not rated by specific reviewers and the count of ratings per movie.

Uploaded by

21ucs037
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/ 13

MariaDB [(none)]> use lab7;

Database changed
MariaDB [lab7]> select * from Movie;
+------+-------------------------+------+------------------+
| mID | title | year | director |
+------+-------------------------+------+------------------+
| 101 | Gone with the Wind | 1939 | Victor Fleming |
| 102 | Star Wars | 1977 | George Lucas |
| 103 | The Sound of Music | 1965 | Robert Wise |
| 104 | E.T. | 1982 | Steven Spielberg |
| 105 | Titanic | 1997 | James Cameron |
| 106 | Snow White | 1937 | NULL |
| 107 | Avatar | 2009 | James Cameron |
| 108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
+------+-------------------------+------+------------------+
8 rows in set (0.000 sec)

MariaDB [lab7]> select * from Rating;


+------+------+-------+------------+
| rID | mID | stars | ratingDate |
+------+------+-------+------------+
| 201 | 101 | 2 | 2011-01-22 |
| 201 | 101 | 4 | 2011-01-27 |
| 202 | 106 | 4 | NULL |
| 203 | 103 | 2 | 2011-01-20 |
| 203 | 108 | 4 | 2011-01-12 |
| 203 | 108 | 2 | 2011-01-30 |
| 204 | 101 | 3 | 2011-01-09 |
| 205 | 103 | 3 | 2011-01-27 |
| 205 | 104 | 2 | 2011-01-22 |
| 205 | 108 | 4 | NULL |
| 206 | 107 | 3 | 2011-01-15 |
| 206 | 106 | 5 | 2011-01-19 |
| 207 | 107 | 5 | 2011-01-20 |
| 208 | 104 | 3 | 2011-01-02 |
+------+------+-------+------------+
14 rows in set (0.000 sec)

MariaDB [lab7]> select * from Reviewer;


+------+------------------+
| rID | name |
+------+------------------+
| 201 | Sarah Martinez |
| 202 | Daniel Lewis |
| 203 | Brittany Harris |
| 204 | Mike Anderson |
| 205 | Chris Jackson |
| 206 | Elizabeth Thomas |
| 207 | James Cameron |
| 208 | Ashley White |
+------+------------------+
8 rows in set (0.000 sec)

MariaDB [lab7]> select r2.name from Reviewer R1, Reviewer R2, Rating r1, Rating r2
where(R1.name='Brittany Harris' and R1.rID=r1.rID and r1.mID=r2.mID and r1.rID!
=r2.rID);
ERROR 1066 (42000): Not unique table/alias: 'r1'
MariaDB [lab7]> select R2.name from Reviewer R1, Reviewer R2, Rating r1, Rating r2
where(R1.name='Brittany Harris' and R1.rID=r1.rID and r1.mID=r2.mID and R1.rID!
=R2.rID);
ERROR 1066 (42000): Not unique table/alias: 'r1'
MariaDB [lab7]> select name from Reviewer r1 where r1.rID in(select R2.rID from
Rating R1, Rating R2, Reviewer where (Reviewer.name='Brittany Harris' and
Reviewer.rID=R1.rID and R1.mID=R2.mID and R1.rID!=R2.rID));
+---------------+
| name |
+---------------+
| Chris Jackson |
+---------------+
1 row in set (0.008 sec)

MariaDB [lab7]> select title from Movie where Movie.mID not in(select mID from
Rating, Reviewer where(name='Chris Jackson' and Reviewer.rID=Rating.rID));
+--------------------+
| title |
+--------------------+
| Gone with the Wind |
| Star Wars |
| Titanic |
| Snow White |
| Avatar |
+--------------------+
5 rows in set (0.007 sec)

MariaDB [lab7]> select * from Movie where mID in(select mID from Rating
where(rID=205 and rID!=203));
+------+-------------------------+------+------------------+
| mID | title | year | director |
+------+-------------------------+------+------------------+
| 103 | The Sound of Music | 1965 | Robert Wise |
| 104 | E.T. | 1982 | Steven Spielberg |
| 108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
+------+-------------------------+------+------------------+
3 rows in set (0.001 sec)

MariaDB [lab7]> select * from Movie where mID in(select r1.mID from Rating r1,
Rating r2 where(r1.rID=205 and r2.rID!=203 and r1.mID=r2.mID));
+------+-------------------------+------+------------------+
| mID | title | year | director |
+------+-------------------------+------+------------------+
| 103 | The Sound of Music | 1965 | Robert Wise |
| 104 | E.T. | 1982 | Steven Spielberg |
| 108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
+------+-------------------------+------+------------------+
3 rows in set (0.001 sec)

MariaDB [lab7]> select * from Movie where mID in(select r1.mID from Rating r1,
Rating r2 where(r1.rID=205 and r2.rID=203 and r1.mID=r2.mID));
+------+-------------------------+------+------------------+
| mID | title | year | director |
+------+-------------------------+------+------------------+
| 103 | The Sound of Music | 1965 | Robert Wise |
| 108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
+------+-------------------------+------+------------------+
2 rows in set (0.001 sec)

MariaDB [lab7]> select * from Movie where (mID in(select mID from Rating
where(rID=205)) and mID not in(select mID from Rating where(rID=203)));
+------+-------+------+------------------+
| mID | title | year | director |
+------+-------+------+------------------+
| 104 | E.T. | 1982 | Steven Spielberg |
+------+-------+------+------------------+
1 row in set (0.001 sec)

MariaDB [lab7]> select * from Movie;


+------+-------------------------+------+------------------+
| mID | title | year | director |
+------+-------------------------+------+------------------+
| 101 | Gone with the Wind | 1939 | Victor Fleming |
| 102 | Star Wars | 1977 | George Lucas |
| 103 | The Sound of Music | 1965 | Robert Wise |
| 104 | E.T. | 1982 | Steven Spielberg |
| 105 | Titanic | 1997 | James Cameron |
| 106 | Snow White | 1937 | NULL |
| 107 | Avatar | 2009 | James Cameron |
| 108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
+------+-------------------------+------+------------------+
8 rows in set (0.000 sec)

MariaDB [lab7]> select * from Rating;


+------+------+-------+------------+
| rID | mID | stars | ratingDate |
+------+------+-------+------------+
| 201 | 101 | 2 | 2011-01-22 |
| 201 | 101 | 4 | 2011-01-27 |
| 202 | 106 | 4 | NULL |
| 203 | 103 | 2 | 2011-01-20 |
| 203 | 108 | 4 | 2011-01-12 |
| 203 | 108 | 2 | 2011-01-30 |
| 204 | 101 | 3 | 2011-01-09 |
| 205 | 103 | 3 | 2011-01-27 |
| 205 | 104 | 2 | 2011-01-22 |
| 205 | 108 | 4 | NULL |
| 206 | 107 | 3 | 2011-01-15 |
| 206 | 106 | 5 | 2011-01-19 |
| 207 | 107 | 5 | 2011-01-20 |
| 208 | 104 | 3 | 2011-01-02 |
+------+------+-------+------------+
14 rows in set (0.000 sec)

MariaDB [lab7]> select * from Reviewer;


+------+------------------+
| rID | name |
+------+------------------+
| 201 | Sarah Martinez |
| 202 | Daniel Lewis |
| 203 | Brittany Harris |
| 204 | Mike Anderson |
| 205 | Chris Jackson |
| 206 | Elizabeth Thomas |
| 207 | James Cameron |
| 208 | Ashley White |
+------+------------------+
8 rows in set (0.000 sec)
MariaDB [lab7]> select director from Movie where count(title)>1 group by title;
ERROR 1111 (HY000): Invalid use of group function
MariaDB [lab7]> select director from Movie where count(title)>1 groupby title;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near
'groupby title' at line 1
MariaDB [lab7]> select director, count(title) from Movie group by title;
+------------------+--------------+
| director | count(title) |
+------------------+--------------+
| James Cameron | 1 |
| Steven Spielberg | 1 |
| Victor Fleming | 1 |
| Steven Spielberg | 1 |
| NULL | 1 |
| George Lucas | 1 |
| Robert Wise | 1 |
| James Cameron | 1 |
+------------------+--------------+
8 rows in set (0.027 sec)

MariaDB [lab7]> select director, count(director) from Movie group by title;


+------------------+-----------------+
| director | count(director) |
+------------------+-----------------+
| James Cameron | 1 |
| Steven Spielberg | 1 |
| Victor Fleming | 1 |
| Steven Spielberg | 1 |
| NULL | 0 |
| George Lucas | 1 |
| Robert Wise | 1 |
| James Cameron | 1 |
+------------------+-----------------+
8 rows in set (0.012 sec)

MariaDB [lab7]> select director, count(director) from Movie group by director;


+------------------+-----------------+
| director | count(director) |
+------------------+-----------------+
| NULL | 0 |
| George Lucas | 1 |
| James Cameron | 2 |
| Robert Wise | 1 |
| Steven Spielberg | 2 |
| Victor Fleming | 1 |
+------------------+-----------------+
6 rows in set (0.010 sec)

MariaDB [lab7]> select director from Movie group by director having count>1;
ERROR 1054 (42S22): Unknown column 'count' in 'having clause'
MariaDB [lab7]> select director from Movie group by director having
count(director)>1;
+------------------+
| director |
+------------------+
| James Cameron |
| Steven Spielberg |
+------------------+
2 rows in set (0.013 sec)

MariaDB [lab7]> select title, count(Rating.mID) from Movie, Rating group by


Rating.mID;
+--------------------+-------------------+
| title | count(Rating.mID) |
+--------------------+-------------------+
| Gone with the Wind | 24 |
| Gone with the Wind | 16 |
| Gone with the Wind | 16 |
| Gone with the Wind | 16 |
| Gone with the Wind | 16 |
| Gone with the Wind | 24 |
+--------------------+-------------------+
6 rows in set (0.012 sec)

MariaDB [lab7]> select title, count(Rating.mID) from Movie, Rating


where(Movie.mID=Rating.mID) group by Rating.mID;
+-------------------------+-------------------+
| title | count(Rating.mID) |
+-------------------------+-------------------+
| Gone with the Wind | 3 |
| The Sound of Music | 2 |
| E.T. | 2 |
| Snow White | 2 |
| Avatar | 2 |
| Raiders of the Lost Ark | 3 |
+-------------------------+-------------------+
6 rows in set (0.015 sec)

MariaDB [lab7]> select stars, count(stars) from Rating where stars in(4, 5) group
by stars;
+-------+--------------+
| stars | count(stars) |
+-------+--------------+
| 4 | 4 |
| 5 | 2 |
+-------+--------------+
2 rows in set (0.001 sec)

MariaDB [lab7]> select title, max(stars) from Movie, Rating where


Movie.mID=Rating.mID group by stars;
+--------------------+------------+
| title | max(stars) |
+--------------------+------------+
| Gone with the Wind | 2 |
| Gone with the Wind | 3 |
| Gone with the Wind | 4 |
| Snow White | 5 |
+--------------------+------------+
4 rows in set (0.012 sec)

MariaDB [lab7]> select * from Movie;


+------+-------------------------+------+------------------+
| mID | title | year | director |
+------+-------------------------+------+------------------+
| 101 | Gone with the Wind | 1939 | Victor Fleming |
| 102 | Star Wars | 1977 | George Lucas |
| 103 | The Sound of Music | 1965 | Robert Wise |
| 104 | E.T. | 1982 | Steven Spielberg |
| 105 | Titanic | 1997 | James Cameron |
| 106 | Snow White | 1937 | NULL |
| 107 | Avatar | 2009 | James Cameron |
| 108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
+------+-------------------------+------+------------------+
8 rows in set (0.001 sec)

MariaDB [lab7]> select * from Rating;


+------+------+-------+------------+
| rID | mID | stars | ratingDate |
+------+------+-------+------------+
| 201 | 101 | 2 | 2011-01-22 |
| 201 | 101 | 4 | 2011-01-27 |
| 202 | 106 | 4 | NULL |
| 203 | 103 | 2 | 2011-01-20 |
| 203 | 108 | 4 | 2011-01-12 |
| 203 | 108 | 2 | 2011-01-30 |
| 204 | 101 | 3 | 2011-01-09 |
| 205 | 103 | 3 | 2011-01-27 |
| 205 | 104 | 2 | 2011-01-22 |
| 205 | 108 | 4 | NULL |
| 206 | 107 | 3 | 2011-01-15 |
| 206 | 106 | 5 | 2011-01-19 |
| 207 | 107 | 5 | 2011-01-20 |
| 208 | 104 | 3 | 2011-01-02 |
+------+------+-------+------------+
14 rows in set (0.000 sec)

MariaDB [lab7]> select * from Reviewer;


+------+------------------+
| rID | name |
+------+------------------+
| 201 | Sarah Martinez |
| 202 | Daniel Lewis |
| 203 | Brittany Harris |
| 204 | Mike Anderson |
| 205 | Chris Jackson |
| 206 | Elizabeth Thomas |
| 207 | James Cameron |
| 208 | Ashley White |
+------+------------------+
8 rows in set (0.000 sec)

MariaDB [lab7]> select title, max(stars) from Movie, Rating where Movie.mID
in(select mID from Rating group by stars);
+--------------------+------------+
| title | max(stars) |
+--------------------+------------+
| Gone with the Wind | 5 |
+--------------------+------------+
1 row in set (0.004 sec)

MariaDB [lab7]> select title, max(stars) from Movie, Rating where


Movie.mID=Rating.mID group by stars, mID;
ERROR 1052 (23000): Column 'mID' in group statement is ambiguous
MariaDB [lab7]> select title, max(stars) from Movie, Rating where
Movie.mID=Rating.mID group by stars, Rating.mID;
+-------------------------+------------+
| title | max(stars) |
+-------------------------+------------+
| Gone with the Wind | 2 |
| The Sound of Music | 2 |
| E.T. | 2 |
| Raiders of the Lost Ark | 2 |
| Gone with the Wind | 3 |
| The Sound of Music | 3 |
| E.T. | 3 |
| Avatar | 3 |
| Gone with the Wind | 4 |
| Snow White | 4 |
| Raiders of the Lost Ark | 4 |
| Snow White | 5 |
| Avatar | 5 |
+-------------------------+------------+
13 rows in set (0.010 sec)

MariaDB [lab7]> select title, max(stars) from Movie, Rating where


Movie.mID=Rating.mID group by stars, Movie.mID;
+-------------------------+------------+
| title | max(stars) |
+-------------------------+------------+
| Gone with the Wind | 2 |
| The Sound of Music | 2 |
| E.T. | 2 |
| Raiders of the Lost Ark | 2 |
| Gone with the Wind | 3 |
| The Sound of Music | 3 |
| E.T. | 3 |
| Avatar | 3 |
| Gone with the Wind | 4 |
| Snow White | 4 |
| Raiders of the Lost Ark | 4 |
| Snow White | 5 |
| Avatar | 5 |
+-------------------------+------------+
13 rows in set (0.011 sec)

MariaDB [lab7]> select title, max(stars) from Movie, Rating where


Movie.mID=Rating.mID group by stars;
+--------------------+------------+
| title | max(stars) |
+--------------------+------------+
| Gone with the Wind | 2 |
| Gone with the Wind | 3 |
| Gone with the Wind | 4 |
| Snow White | 5 |
+--------------------+------------+
4 rows in set (0.013 sec)

MariaDB [lab7]> select title, max(stars) from Movie, Rating where


Movie.mID=Rating.mID group by Movie.mID;
+-------------------------+------------+
| title | max(stars) |
+-------------------------+------------+
| Gone with the Wind | 4 |
| The Sound of Music | 3 |
| E.T. | 3 |
| Snow White | 5 |
| Avatar | 5 |
| Raiders of the Lost Ark | 4 |
+-------------------------+------------+
6 rows in set (0.019 sec)

MariaDB [lab7]> select title, max(stars) from Movie, Rating where


Movie.mID=Rating.mID group by Movie.mID order by title;
+-------------------------+------------+
| title | max(stars) |
+-------------------------+------------+
| Avatar | 5 |
| E.T. | 3 |
| Gone with the Wind | 4 |
| Raiders of the Lost Ark | 4 |
| Snow White | 5 |
| The Sound of Music | 3 |
+-------------------------+------------+
6 rows in set (0.010 sec)

MariaDB [lab7]> select title, max(stars)-min(stars) as spread from Movie, Rating


where Movie.mID=Rating.mID group by Movie.mID order by title;
+-------------------------+--------+
| title | spread |
+-------------------------+--------+
| Avatar | 2 |
| E.T. | 1 |
| Gone with the Wind | 2 |
| Raiders of the Lost Ark | 2 |
| Snow White | 1 |
| The Sound of Music | 1 |
+-------------------------+--------+
6 rows in set (0.012 sec)

MariaDB [lab7]> select title, max(stars)-min(stars) as spread from Movie, Rating


where Movie.mID=Rating.mID group by Movie.mID order by spread, title;
+-------------------------+--------+
| title | spread |
+-------------------------+--------+
| E.T. | 1 |
| Snow White | 1 |
| The Sound of Music | 1 |
| Avatar | 2 |
| Gone with the Wind | 2 |
| Raiders of the Lost Ark | 2 |
+-------------------------+--------+
6 rows in set (0.010 sec)

MariaDB [lab7]> select title, max(stars)-min(stars) as spread from Movie, Rating


where Movie.mID=Rating.mID group by Movie.mID order by spread desc, title ;
+-------------------------+--------+
| title | spread |
+-------------------------+--------+
| Avatar | 2 |
| Gone with the Wind | 2 |
| Raiders of the Lost Ark | 2 |
| E.T. | 1 |
| Snow White | 1 |
| The Sound of Music | 1 |
+-------------------------+--------+
6 rows in set (0.012 sec)

MariaDB [lab7]> select * from Movie;


+------+-------------------------+------+------------------+
| mID | title | year | director |
+------+-------------------------+------+------------------+
| 101 | Gone with the Wind | 1939 | Victor Fleming |
| 102 | Star Wars | 1977 | George Lucas |
| 103 | The Sound of Music | 1965 | Robert Wise |
| 104 | E.T. | 1982 | Steven Spielberg |
| 105 | Titanic | 1997 | James Cameron |
| 106 | Snow White | 1937 | NULL |
| 107 | Avatar | 2009 | James Cameron |
| 108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
+------+-------------------------+------+------------------+
8 rows in set (0.001 sec)

MariaDB [lab7]> select * from Rating;


+------+------+-------+------------+
| rID | mID | stars | ratingDate |
+------+------+-------+------------+
| 201 | 101 | 2 | 2011-01-22 |
| 201 | 101 | 4 | 2011-01-27 |
| 202 | 106 | 4 | NULL |
| 203 | 103 | 2 | 2011-01-20 |
| 203 | 108 | 4 | 2011-01-12 |
| 203 | 108 | 2 | 2011-01-30 |
| 204 | 101 | 3 | 2011-01-09 |
| 205 | 103 | 3 | 2011-01-27 |
| 205 | 104 | 2 | 2011-01-22 |
| 205 | 108 | 4 | NULL |
| 206 | 107 | 3 | 2011-01-15 |
| 206 | 106 | 5 | 2011-01-19 |
| 207 | 107 | 5 | 2011-01-20 |
| 208 | 104 | 3 | 2011-01-02 |
+------+------+-------+------------+
14 rows in set (0.000 sec)

MariaDB [lab7]> select * from Reviewer;


+------+------------------+
| rID | name |
+------+------------------+
| 201 | Sarah Martinez |
| 202 | Daniel Lewis |
| 203 | Brittany Harris |
| 204 | Mike Anderson |
| 205 | Chris Jackson |
| 206 | Elizabeth Thomas |
| 207 | James Cameron |
| 208 | Ashley White |
+------+------------------+
8 rows in set (0.001 sec)

MariaDB [lab7]> select title from Movie where mID in(select mID from Rating group
by stars having count(stars)>max(stars));
+--------------------+
| title |
+--------------------+
| Gone with the Wind |
+--------------------+
1 row in set (0.004 sec)

MariaDB [lab7]> select title from Movie where mID in(select mID from Rating group
by mID having count(stars)>=max(stars));
Empty set (0.001 sec)

MariaDB [lab7]> select title from Movie where mID in(select mID from Rating where
stars>=max(stars) group by mID;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near '' at
line 1
MariaDB [lab7]> select title from Movie where mID in(select mID from Rating where
stars>=max(stars) group by mID);
ERROR 1111 (HY000): Invalid use of group function
MariaDB [lab7]> select title from Movie where mID in(select mID from Rating group
by mID having stars>=max(stars);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near '' at
line 1
MariaDB [lab7]> select title from Movie where mID in(select mID from Rating group
by mID having stars>=max(stars));
ERROR 1054 (42S22): Unknown column 'stars' in 'having clause'
MariaDB [lab7]> select title from Movie where mID in(select mID from Rating where
stars>=5);
+------------+
| title |
+------------+
| Snow White |
| Avatar |
+------------+
2 rows in set (0.001 sec)

.
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near '.' at
line 1
MariaDB [lab7]> select * from Movie;
+------+-------------------------+------+------------------+
| mID | title | year | director |
+------+-------------------------+------+------------------+
| 101 | Gone with the Wind | 1939 | Victor Fleming |
| 102 | Star Wars | 1977 | George Lucas |
| 103 | The Sound of Music | 1965 | Robert Wise |
| 104 | E.T. | 1982 | Steven Spielberg |
| 105 | Titanic | 1997 | James Cameron |
| 106 | Snow White | 1937 | NULL |
| 107 | Avatar | 2009 | James Cameron |
| 108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
+------+-------------------------+------+------------------+
8 rows in set (0.000 sec)

MariaDB [lab7]> select * from Rating;


+------+------+-------+------------+
| rID | mID | stars | ratingDate |
+------+------+-------+------------+
| 201 | 101 | 2 | 2011-01-22 |
| 201 | 101 | 4 | 2011-01-27 |
| 202 | 106 | 4 | NULL |
| 203 | 103 | 2 | 2011-01-20 |
| 203 | 108 | 4 | 2011-01-12 |
| 203 | 108 | 2 | 2011-01-30 |
| 204 | 101 | 3 | 2011-01-09 |
| 205 | 103 | 3 | 2011-01-27 |
| 205 | 104 | 2 | 2011-01-22 |
| 205 | 108 | 4 | NULL |
| 206 | 107 | 3 | 2011-01-15 |
| 206 | 106 | 5 | 2011-01-19 |
| 207 | 107 | 5 | 2011-01-20 |
| 208 | 104 | 3 | 2011-01-02 |
+------+------+-------+------------+
14 rows in set (0.000 sec)

MariaDB [lab7]> select * from Reviewer;


+------+------------------+
| rID | name |
+------+------------------+
| 201 | Sarah Martinez |
| 202 | Daniel Lewis |
| 203 | Brittany Harris |
| 204 | Mike Anderson |
| 205 | Chris Jackson |
| 206 | Elizabeth Thomas |
| 207 | James Cameron |
| 208 | Ashley White |
+------+------------------+
8 rows in set (0.000 sec)

MariaDB [lab7]> select title from Movie where mID in(select mID from Rating where
(stars in(select max(stars) from Rating where rID=205 group by rID));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near '' at
line 1
MariaDB [lab7]> select title from Movie where mID in(select mID from Rating where
(stars in(select max(stars) from Rating where rID=205 group by rID)));
+-------------------------+
| title |
+-------------------------+
| Gone with the Wind |
| Snow White |
| Raiders of the Lost Ark |
+-------------------------+
3 rows in set (0.001 sec)

MariaDB [lab7]> select title from Movie where mID in(select mID from Rating where
(stars >(select max(stars) from Rating where rID=205 group by rID)));
+------------+
| title |
+------------+
| Snow White |
| Avatar |
+------------+
2 rows in set (0.001 sec)

MariaDB [lab7]> select mID, title from Movie where mID in(select mID from Rating
where ((select count(mID) from Rating group by m
ID)=(select max(mID) from Rating group by mID)));
ERROR 1242 (21000): Subquery returns more than 1 row
MariaDB [lab7]> select mID, title from Movie where mID in(select mID from Rating
having count(mID)=(select max(mID) from Rating group by mID) group by mID);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near 'group
by mID)' at line 1
MariaDB [lab7]> select mID, title from Movie where mID in(select mID from Rating
group by mID having count(mID)=(select max(mID) from Rating group by mID));
ERROR 1242 (21000): Subquery returns more than 1 row
MariaDB [lab7]> select mID, title from Movie where mID in(select mID from Rating
group by mID having count(mID) in(select max(mID) from Rating group by mID));
Empty set (0.001 sec)

MariaDB [lab7]> select * from Movie;


+------+-------------------------+------+------------------+
| mID | title | year | director |
+------+-------------------------+------+------------------+
| 101 | Gone with the Wind | 1939 | Victor Fleming |
| 102 | Star Wars | 1977 | George Lucas |
| 103 | The Sound of Music | 1965 | Robert Wise |
| 104 | E.T. | 1982 | Steven Spielberg |
| 105 | Titanic | 1997 | James Cameron |
| 106 | Snow White | 1937 | NULL |
| 107 | Avatar | 2009 | James Cameron |
| 108 | Raiders of the Lost Ark | 1981 | Steven Spielberg |
+------+-------------------------+------+------------------+
8 rows in set (0.001 sec)

MariaDB [lab7]> select * from Rating;


+------+------+-------+------------+
| rID | mID | stars | ratingDate |
+------+------+-------+------------+
| 201 | 101 | 2 | 2011-01-22 |
| 201 | 101 | 4 | 2011-01-27 |
| 202 | 106 | 4 | NULL |
| 203 | 103 | 2 | 2011-01-20 |
| 203 | 108 | 4 | 2011-01-12 |
| 203 | 108 | 2 | 2011-01-30 |
| 204 | 101 | 3 | 2011-01-09 |
| 205 | 103 | 3 | 2011-01-27 |
| 205 | 104 | 2 | 2011-01-22 |
| 205 | 108 | 4 | NULL |
| 206 | 107 | 3 | 2011-01-15 |
| 206 | 106 | 5 | 2011-01-19 |
| 207 | 107 | 5 | 2011-01-20 |
| 208 | 104 | 3 | 2011-01-02 |
+------+------+-------+------------+
14 rows in set (0.000 sec)

MariaDB [lab7]> select * from Reviewer;


+------+------------------+
| rID | name |
+------+------------------+
| 201 | Sarah Martinez |
| 202 | Daniel Lewis |
| 203 | Brittany Harris |
| 204 | Mike Anderson |
| 205 | Chris Jackson |
| 206 | Elizabeth Thomas |
| 207 | James Cameron |
| 208 | Ashley White |
+------+------------------+
8 rows in set (0.000 sec)

MariaDB [lab7]> select mID, count(mID) from Rating group by mID;


+------+------------+
| mID | count(mID) |
+------+------------+
| 101 | 3 |
| 103 | 2 |
| 104 | 2 |
| 106 | 2 |
| 107 | 2 |
| 108 | 3 |
+------+------------+
6 rows in set (0.001 sec)

MariaDB [lab7]> select mID, max(mID) from Rating group by mID;


+------+----------+
| mID | max(mID) |
+------+----------+
| 101 | 101 |
| 103 | 103 |
| 104 | 104 |
| 106 | 106 |
| 107 | 107 |
| 108 | 108 |
+------+----------+
6 rows in set (0.001 sec)

MariaDB [lab7]> select mID, max(count(mID)) from Rating group by mID;


ERROR 1111 (HY000): Invalid use of group function
MariaDB [lab7]> select mID, count(mID) from Rating group by mID;
+------+------------+
| mID | count(mID) |
+------+------------+
| 101 | 3 |
| 103 | 2 |
| 104 | 2 |
| 106 | 2 |
| 107 | 2 |
| 108 | 3 |
+------+------------+
6 rows in set (0.001 sec)

You might also like