You are on page 1of 4

SQL LAB 2

SCHEMA:
College(cName text, state text, enrollment int)
Student(sID int, sName text, GPA real, sizeHS int)
Apply(sID int, cName text, major text, decision text)

TABLE DATA

Student
123, 'Amy', 3.9, 1000
234, 'Bob', 3.6, 1500
345, 'Craig', 3.5, 500
456, 'Doris', 3.9, 1000
567, 'Edward', 2.9, 2000
678, 'Fay', 3.8, 200
789, 'Gary', 3.4, 800
987, 'Helen', 3.7, 800
876, 'Irene', 3.9, 400
765, 'Jay', 2.9, 1500
654, 'Amy', 3.9, 1000
543, 'Craig', 3.4, 2000

College
'Stanford', 'CA', 15000
'Berkeley', 'CA', 36000
'MIT', 'MA', 10000
'Cornell', 'NY', 21000

Apply
123, 'Stanford', 'CS', 'Y'
123, 'Stanford', 'EE', 'N'
123, 'Berkeley', 'CS', 'Y'
123, 'Cornell', 'EE', 'Y'
234, 'Berkeley', 'biology', 'N'
345, 'MIT', 'bioengineering', 'Y'
345, 'Cornell', 'bioengineering', 'N'
345, 'Cornell', 'CS', 'Y'
345, 'Cornell', 'EE', 'N'
678, 'Stanford', 'history', 'Y'
987, 'Stanford', 'CS', 'Y'
987, 'Berkeley', 'CS', 'Y'
876, 'Stanford', 'CS', 'N'
876, 'MIT', 'biology', 'Y'
876, 'MIT', 'marine biology', 'N'
765, 'Stanford', 'history', 'Y'
765, 'Cornell', 'history', 'N'
765, 'Cornell', 'psychology', 'Y'
543, 'MIT', 'CS', 'N'
QUESTION AND EXPECTED RESULT

/**************************************************************
SUBQUERIES IN THE WHERE CLAUSE
**************************************************************/
Set Membership
******************************************************************
Ques 1. IDs and names of students applying to CS
USE -- IN CLAUSE
**************************************************************/

+------+-------+
| sID | sName |
+------+-------+
| 123 | Amy |
| 345 | Craig |
| 987 | Helen |
| 876 | Irene |
| 543 | Craig |
+------+-------+
5 rows in set (0.01 sec)

/**************************************************************
Q.2 Students who applied to CS but not EE
HINT -- USE -- IN
**************************************************************/

+------+-------+
| sID | sName |
+------+-------+
| 987 | Helen |
| 876 | Irene |
| 543 | Craig |
+------+-------+
3 rows in set (0.00 sec)

/**************************************************************
Q3. Colleges such that some other college is in the same state
HINT : USE -- EXISTS
**************************************************************/

+----------+
| cName |
+----------+
| Stanford |
| Berkeley |
+----------+
2 rows in set (0.02 sec)
/**************************************************************
Q4. Student with highest GPA
USE -- EXISTS
**************************************************************/

+------+-------+------+
| sID | sName | GPA |
+------+-------+------+
| 123 | Amy | 3.9 |
| 456 | Doris | 3.9 |
| 876 | Irene | 3.9 |
| 654 | Amy | 3.9 |
+------+-------+------+
4 rows in set (0.01 sec)

--- SET COMPARISION


/****************************************************************
Q5. Highest GPA using ">= all"
**************************************************************/

+------+-------+------+
| sID | sName | GPA |
+------+-------+------+
| 123 | Amy | 3.9 |
| 456 | Doris | 3.9 |
| 876 | Irene | 3.9 |
| 654 | Amy | 3.9 |
+------+-------+------+
4 rows in set (0.01 sec)

/**************************************************************
Q6. Students not from the smallest HS
**************************************************************/

+------+--------+------+--------+
| sID | sName | GPA | sizeHS |
+------+--------+------+--------+
| 123 | Amy | 3.9 | 1000 |
| 234 | Bob | 3.6 | 1500 |
| 345 | Craig | 3.5 | 500 |
| 456 | Doris | 3.9 | 1000 |
| 567 | Edward | 2.9 | 2000 |
| 789 | Gary | 3.4 | 800 |
| 987 | Helen | 3.7 | 800 |
| 876 | Irene | 3.9 | 400 |
| 765 | Jay | 2.9 | 1500 |
| 654 | Amy | 3.9 | 1000 |
| 543 | Craig | 3.4 | 2000 |
+------+--------+------+--------+
11 rows in set (0.00 sec)
/**************************************************************
SUBQUERIES IN THE FROM AND SELECT CLAUSES
**************************************************************/

/*
Q7. Students whose scaled GPA changes GPA by more than 1(either
increase/decrease)
Scaled GPA = GPA*(sizeHS/1000.0)
SOLVE BY SUBQUERY IN FROM CLAUSE
**************************************************************/

+------+--------+------+-----------+
| sID | sName | GPA | ScaledGPA |
+------+--------+------+-----------+
| 234 | Bob | 3.6 | 5.40 |
| 345 | Craig | 3.5 | 1.75 |
| 567 | Edward | 2.9 | 5.80 |
| 678 | Fay | 3.8 | 0.76 |
| 876 | Irene | 3.9 | 1.56 |
| 765 | Jay | 2.9 | 4.35 |
| 543 | Craig | 3.4 | 6.80 |
+------+--------+------+-----------+
7 rows in set (0.00 sec)

/**************************************************************
Q8. Colleges paired with the highest GPA of their applicants
SOLVE BY SUBQUERY IN SELECT CLAUSE
**************************************************************/

+----------+-------+------+
| cName | state | GPA |
+----------+-------+------+
| Stanford | CA | 3.9 |
| Berkeley | CA | 3.9 |
| MIT | MA | 3.9 |
| Cornell | NY | 3.9 |
+----------+-------+------+
4 rows in set (0.00 sec)

You might also like