2. General SELECT statement
Select <List of Columns and expressions (usually involving
columns)>
From <List of Tables & Join Operators>
Where <List of Row conditions joined together by And, Or,
Not>
Group By <list of grouping columns>
Having <list of group conditions connected by And, Or, Not >
Order By <list of sorting specifications>
3. Joins
• We can refer to multiple tables using SELECT.
• There are 5 types of JOIN:
• INNER JOIN
• FULL OUTER JOIN
• LEFT OUTER JOIN
• RIGHT OUTER JOIN
• CROSS JOIN
6. INNER JOIN
• This is one of the more frequent types of joins
• Finds all rows which meet the join condition
• Create the result-set by combining all rows from both the tables
where the condition satisfies i.e value of the common field will be
same.
7. Syntax: INNER JOIN
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
• Here:
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
• We can also write JOIN instead of INNER JOIN. JOIN is same as INNER
JOIN.
8. Example: INNER JOIN
• Write an SQL query to show the names and age of students enrolled
in different courses.
SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM
Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
9. LEFT OUTER JOIN
• Contains all records between T1 and T2 that meet the join condition,
and then any records in T1 that don’t meet the join condition
• Rows for which the join condition is not met, the columns of T2 are padded
with nulls
• Also known as Left Join
10. Syntax: LEFT OUTER JOIN
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT OUTER JOIN table2
ON table1.matching_column = table2.matching_column;
• Here:
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
• We can also write LEFT JOIN instead of LEFT OUTER JOIN.
11. Example: LEFT OUTER JOIN
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
12. RIGHT OUTER JOIN
• Contains all records between T1 and T2 that meet the join condition,
and then any records in T2 that don’t meet the join condition
• Rows for which the join condition is not met, the columns of T1 are padded
with nulls
• Also known as RIGHT JOIN
13. Syntax: RIGHT OUTER JOIN
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT OUTER JOIN table2
ON table1.matching_column = table2.matching_column;
• Here:
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
• We can also write RIGHT JOIN instead of RIGHT OUTER JOIN.
14. FULL OUTER JOIN
• Contains all records between T1 and T2 that meet the join condition,
and the combination of a left outer join and right outer join are
appended onto the results.
• The rows for which there is no matching, the result-set will
contain NULL values.
15. Syntax: FULL OUTER JOIN
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL OUTER JOIN table2
ON table1.matching_column = table2.matching_column;
• Here:
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
• We can also write FULL JOIN instead of FULL OUTER JOIN.
16. Cross join
• Cross joins have no join condition
• Cross joins literally return the Cartesian product of the rows.
17. Syntax: CROSS JOIN
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
CROSS JOIN table2;
• Here:
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
• Note that there is no ON keyword.
• Try to add WHERE clause at the end and see results.
18. Explicit vs Implicit Joins
• Explicit Joins
SELECT * FROM AddressBook a
INNER JOIN CallList c on a.id = c.addressBookId;
• Implicit Join
SELECT * FROM AddressBook a, CallList c
WHERE a.id = c.addressBookId;
• You can use any one you like.
19. Views
•A view is kind of like a virtual table
• Views are defined as queries (SELECT statements)
• They can be queried like tables
20. Syntax: VIEWS
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
• Here:
• view_name: Name for the View
• table_name: Name of the table
• condition: Condition to select rows
21. View example
CREATE VIEW StudentNames AS
SELECT Roll_no, name FROM Student
WHERE Roll_no > 3
• This would create a view that contains names and roll
number from table: Student
• You can further query this view. For example:
SELECT * FROM StudentNames;
• Similarly views can be created from different tables.
22. Questions
1. Write a query that returns the account ID for each
nonbusiness customer (customer.cust_type_cd = 'I')
with the customer’s federal ID (customer.fed_id) and
the name of the product on which the account is
based (product.name).
2. Construct a query that finds all employees whose
supervisor is assigned to a different department.
Retrieve the employees’ ID, first name, and last
name.