SQL Lab-7
SQL Lab-7
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 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 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 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
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 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 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)