Lab Assignment 4
Lab Assignment 4
3
Environment: Microsoft Windows
Tools/ Language: Oracle/SQL
Objective: Write the SQL queries using Set Operations and Joins.
SQL JOINS are used to retrieve data from multiple tables. A SQL JOIN is performed
whenever two or more tables are joined in a SQL statement.
Syntax
The syntax for the SQL INNER JOIN is:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
If the tables COUNTRIES and CITIES have two common columns named
POPULATION and COUNTRY_ISO_CODE, JOIN applies equality condition on ISO
codes with cities having less POPULATION attributes:
SELECT * FROM
COUNTRIES
INNER JOIN CITIES
On COUNTRIES.COUNTRY_ISO_CODE = CITIES.COUNTRY_ISO_CODE
And COUNTRIES.POPULATION > CITIES.POPULATION;
Syntax
In some databases, the LEFT OUTER JOIN keywords are replaced with LEFT JOIN.
SELECT * FROM
COUNTRIES
LEFT JOIN CITIES
On COUNTRIES.COUNTRY_ISO_CODE=CITIES. COUNTRY_ISO_CODE
And COUNTRIES.POPULATION >CITIES.POPULATION;
In some databases, the RIGHT OUTER JOIN keywords are replaced with RIGHT JOIN.
SELECT * FROM
COUNTRIES
RIGHT JOIN CITIES
On COUNTRIES.COUNTRY_ISO_CODE=CITIES. COUNTRY_ISO_CODE
And COUNTRIES.POPULATION >CITIES.POPULATION;
Syntax
The syntax for the SQL FULL OUTER JOIN is:
SELECT columns
FROM table1
FULL [OUTER] JOIN table2
ON table1.column = table2.column;
In some databases, the FULL OUTER JOIN keywords are replaced with FULL JOIN.
SELECT * FROM
COUNTRIES
FULL JOIN CITIES
On COUNTRIES.COUNTRY_ISO_CODE=CITIES. COUNTRY_ISO_CODE
And COUNTRIES.POPULATION >CITIES.POPULATION;
A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you
based on the common columns in the two tables being joined. Common columns are
columns that have the same name in both tables.
If the SELECT statement in which the NATURAL JOIN operation appears has an
asterisk (*) in the select list, the asterisk will be expanded to the following list of
columns (in this order):
Syntax
Select *
FROM table1
NATURAL JOIN table2;
Examples
If the tables COUNTRIES and CITIES have two common columns named COUNTRY
and COUNTRY_ISO_CODE, NATURAL JOIN applies equality condition on both
attributes:
A CROSS JOIN is a JOIN operation that produces the Cartesian product of two tables.
Unlike other JOIN operators, it does not let you specify a join clause. You may,
however, specify a WHERE clause in the SELECT statement.
Examples
BEGIN
FOR cur_rec IN (SELECT object_name, object_type
FROM user_objects
WHERE object_type IN
('TABLE',
'VIEW',
'PACKAGE',
'PROCEDURE',
'FUNCTION',
'SEQUENCE'
))
LOOP
BEGIN
IF cur_rec.object_type = 'TABLE'
THEN
EXECUTE IMMEDIATE 'DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '" CASCADE CONSTRAINTS';
ELSE
EXECUTE IMMEDIATE 'DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '"';
END IF;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line ( 'FAILED: DROP '
|| cur_rec.object_type
|| ' "'
|| cur_rec.object_name
|| '"'
);
END;
END LOOP;
END;
/
commit;
drop table College;
drop table Student;
drop table Apply;
Q1. Produce a combine table in which each student is combine with every other
application.
Q2. Give Student ID, name, GPA and name of college and major each student applied to.
Q3. Find detail of applications who applied to California State.
Q4. IDs, name, GPA of students and name of college with GPA > 3.7 applying to
Stanford
Q5. Find detail of Student who apply to CS major and their application are rejected
Q6. Find detail of student and application who applied to colleges at New York
Q7. Find detail of student who have not applied to any of college
Q8. Find college where no student have applied
Q9. Find sID who have only one application
Q10. Find name and GPA of applicants who apply to any college whose enrollment is not
more than 25000.
Q11. Find pair of students (sID) having same GPA. (each pair should occur just once in
result)
Q12. Find various majors student applied in at college in state MA.
Exercise