Lecture 10 SQL Continued JOINS and ALIASES
Lecture 10 SQL Continued JOINS and ALIASES
• In database management an aggregate function is a function where the values of multiple rows
are grouped together as input on certain criteria to form a single value of more significant
meaning.
• Count()
• Min()
• Max()
• Avg()
• Sum()
Example Count():
Count(salary): Return number of Non Null values over the column salary. i.e 5.
Count(Distinct Salary): Return number of distinct Non Null values over the column salary .i.e 4
Sum():
sum(salary): Sum all Non Null values of Column salary i.e., 310
Avg():
Min(salary): Minimum value in the salary column except NULL i.e., 40.
• COLUMN ALIASES are used to make column headings in result set easier to read.
• TABLE ALIASES are used to shorten SQL query to make it easier to read or when there are more than one table is
involved
• An alias only exists for the duration of that query i.e. the renaming is just a temporary change
and the actual table name does not change in the database.
• This can be a good thing to do if you have very long or complex table names or column names.
• These are preferred when there are more than one table involved in a query.
Example: Column Alias
Example: Table Alias
Joins
• SQL joins are used to query data from two or more tables, based on a relationship
between certain columns in these tables.
Need of Joins
Employee Department FK
E_No E_Name Address Dept_No Dept_Name E_No
1 Ram Delhi D1 HR 1
2 Varun Chd D2 IT 2
3 Ravi Chd D3 Marketing 4
4 Amit Delhi D4 Finance 5
5 Nitin Noida
Find employee names who are working in HR department ? To answer this question you need to
understand JOINS
In order to implement JOINS there should be at least one
common attributes between the two tables.
1 Ram D1 1
2 Varun D2 2
Select E_Name from Employee, Department where Employee.E_No. = Department.E_No and Employee.
Address = Department. Location;
E_No E_Name Address Dept_No Location E_No
Select E_Name from Employee, Department where Employee.E_No. = Department.E_No and Employee.
Address = Department. Location;
INNER JOIN/ SIMPLE JOIN
• The INNER JOIN keyword selects all rows from both the tables as long as the condition is satisfied.
• This keyword will 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 the same.
• Only the rows that meet the join condition from both tables are returned. If a row in one table
does not have a matching row in the other table, that row will not be included in the result set.
Example
Student
StudentCourse
Output
• An Outer Join returns all the rows from one table and matching rows from the other table based on
a specified condition.
• It combines data from two tables based on a common column between them, which is also
specified using the ON keyword in SQL.
• In addition to the matching rows, it also includes rows from one table that do not have matching
rows in the other table.
Employee Department
Select E_No.,E_Name, Dept_name, Location from Employee LEFT OUTER JOIN Department ON
Employee. Dept_No= Department. Dept_No;
Employee Department
Select E_No.,E_Name, Dept_name, Location from Employee RIGHT OUTER JOIN Department ON
Employee. Dept_No= Department. Dept_No;
• The result-set will contain all the rows from both tables. For the rows for which there is no
matching, the result-set will contain NULL values.
Another Example
Inner Join Vs Natural Join
Brainstorming Questions
35
Practice Question 1 (Conti.)
1. Create the table orders with columns order_no number type, purch_amt number
(precision, scale), ord_date date, customer_id number and salesman_id number
2. Insert the values as given in the table
3. Add customer name, email address and contact_number columns in the given table
4. Add column gender in the table with a single character value.
5. Update the values of newly added columns in the records.
6. Create another table orders_completed with ord_no, purch_amt, ord_date and
customer name, email_address and contact number. Then copy the information of
details from the orders table where the date is 10th October 2012.
36
Practice Question 2
37
Practice Question 2 (Conti.)
38
Thank you
39