Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 39

SQL Concepts: Joins and Aliases

Aggregate functions in SQL

• 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.

• Various Aggregate Functions

• Count()
• Min()
• Max()
• Avg()
• Sum()
Example Count():

Count(*): Returns total number of records .i.e 6.

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

sum(Distinct salary): Sum of all distinct Non-Null values i.e., 250.

Avg():

Avg(salary) = Sum(salary) / count(salary) = 310/5

Avg(Distinct salary) = sum(Distinct salary) / Count(Distinct Salary) = 250/4


Example

Min(salary): Minimum value in the salary column except NULL i.e., 40.

Max(salary): Maximum value in the salary i.e., 80.


Aliases

• Aliases are used to give a table, or a column in a table, a temporary name.

• 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 is created to make table or column names easier to read.

• 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.

• An alias is created with the AS keyword.

• 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

Print address employee whose name is varun

Print name of department whose department number is D2

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.

JOIN = Cross Product + Select Statement (some condition)


Different types of Joins are as follows:

• Cross Join (Cross Product)


• Natural Join
• Equii Join
• Self Join
• Inner join
• Outer join
• Left outer join
• Right outer join
• Full join
Natural join
• Natural join is an SQL join operation that creates a join on the base of the common columns in
the tables.
• To perform natural join there must be one common attribute(Column) between two tables.
Natural join will retrieve from multiple relations.
• Column name between two tables should be same while implementing natural join
Example
E_No E_Name Dept_No E_No

Employee Department 1 Ram D1 1


FK
1 Ram D2 2
E_No E_Name Address Dept_No Dept_Name E_No
1 Ram D3 4

1 Ram Delhi D1 HR 1 2 Varun D1 1


2 Varun D2 2
2 Varun Chd D2 IT 2
2 Varun D3 4
3 Ravi Chd D3 Marketing 4 3 Ravi D1 1
3 Ravi D2 2
4 Amit Delhi
3 Ravi D3 4
4 Amit D1 1
Print address employee whose name is varun
4 Amit D2 2
4 Amit D3 4
Print name of department whose department number is D2

Find employee names who are working in HR department ?


To answer this question you need to understand JOINS
E_No E_Name Dept_No E_No
Example 1 Ram D1 1
1 Ram D2 2
1 Ram D3 4
2 Varun D1 1
2 Varun D2 2
2 Varun D3 4
3 Ravi D1 1
3 Ravi D2 2
3 Ravi D3 4
4 Amit D1 1
4 Amit D2 2
4 Amit D3 4 E_No E_Name Dept_No E_No

1 Ram D1 1
2 Varun D2 2

Find employee names who are working in department ? 4 Amit D3 4

To answer this question you need to understand JOINS

Select E_Name from Employee,Department where Employee.E_No = Department.E_No;

Select E_Name from Employee Natural Join Department;


Self Join
• A self join is a regular join that is used to join a table with itself.
• It basically allows us to combine the rows from the same table based on some specific conditions.
• It is very useful and easy to work with, and it allows us to retrieve data or information which
involves comparing records within the same table.
• Consider a below relationship table named Study between Student and Course entities with S_id
and C_id acting as foreign keys.
Study

S_id C_id Since


S1 C1 2016
S2 C2 2017
S1 C2 2017

Find S_id who are enrolled in atleast two courses ?


Alias is created
Study (T1) Study (T2)

S_id C_id Since S_id C_id Since


S1 C1 2016 S1 C1 2016
S2 C2 2017 S2 C2 2017
S1 C2 2017
S1 C2 2017

S_id C_id S_id C_id


S1 C1 S1 C1
Select------from study as T1, study as T2 where T1.S_id =T2.S_id and T1.C_id
S1 C1 S2 C2 <>T2.C_id
S1 C1 S1 C2
S_id C_id S_id C_id
S2 C2 S1 C1 S1 C1 S1 C2
S2 C2 S2 C2 S1 C2 S1 C1
S2 C2 S1 C2
S1 C2 S1 C1
S1 C2 S2 C2 Select T1.S_id from study as T1, study as T2 where T1.S_id =T2.S_id and
T1.C_id <>T2.C_id
S1 C2 S1 C2
Equii Join
• Equijoin is a classified type of inner join that returns output by performing joining operations from two
tables based on the common column that exists in them.
• This join returns only those data that are available in both tables based on the common primary field name.
• It does not display the null records or unmatchable data into the result set.
• Find the name of employees who worked in a department having location same
as their address ?
E_No E_Name Address Dept_No Location E_No
Employee

E_No E_Name Address 1 Ram Delhi D1 Delhi 1


1 Ram Delhi
1 Ram Delhi D2 Pune 2
2 Varun Chd
1 Ram Delhi D3 Patna 4
3 Ravi Chd
4 Amit Delhi 2 Varun Chd D1 Delhi 1

2 Varun Chd D2 Pune 2

Department 2 Varun Chd D3 Patna 4

Dept_No Location E_No 3 Ravi Chd D1 Delhi 1

3 Ravi Chd D2 Pune 2


D1 Delhi 1
3 Ravi Chd D3 Patna 4
D2 Pune 2
4 Amit Delhi D1 Delhi 1
D3 Patna 4 4 Amit Delhi D2 Pune 2

4 Amit Delhi D3 Patna 4

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

1 Ram Delhi D1 Delhi 1

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

SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE FROM Student


INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
Outer Join

• 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.

• Outer Join is of three types:


• Left outer join
• Right outer join
• Full Join
Left Outer Join/Left Join
• A SQL operation that combines two tables, showing all rows from the left table and matching
rows from the right table.
• If there is no match in the right table, it displays NULL values.
Example: Left Outer Join

Employee Department

E_No E_Name Dept_No Dept_No Dept_name Location


E1 Varun D1
D1 IT Delhi
E2 Amrit D2
D2 HR Hyderabad
E3 Ravi D1
D3 Finance Pune
E4 Nitin -

Select E_No.,E_Name, Dept_name, Location from Employee LEFT OUTER JOIN Department ON
Employee. Dept_No= Department. Dept_No;

E_No E_Name Dept_name Location


E1 Varun IT Delhi
E2 Amrit HR Hyderabad
E3 Ravi IT Delhi
E4 Nitin - -
Another Example
Right Outer Join/Right Join
• RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the right side of
the join and matching rows for the table on the left side of the join.
• For the rows for which there is no matching row on the left side, the result-set will contain null.
• RIGHT JOIN is also known as RIGHT OUTER JOIN.
Example: Right Outer Join

Employee Department

E_No E_Name Dept_No Dept_No Dept_name Location


E1 Varun D1
D1 IT Delhi
E2 Amrit D2
D2 HR Hyderabad
E3 Ravi D3
D3 Finance Pune
D4 Testing Noida

Select E_No.,E_Name, Dept_name, Location from Employee RIGHT OUTER JOIN Department ON
Employee. Dept_No= Department. Dept_No;

E_No E_Name Dept_name Location


E1 Varun IT Delhi
E2 Amrit HR Hyderabad
E3 Ravi Finance Pune
- - Testing Noida
Another Example
Full Join
• FULL JOIN creates the result-set by combining results of both LEFT JOIN and RIGHT JOIN.

• 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

Difference between the following terms

• Equii Join Vs Natural Join


• Inner Join Vs Natural Join
• Outer Join Vs Inner Join
• Left Join Vs Right Join
Practice Question 1

Consider the following schema

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

Consider the following schema

37
Practice Question 2 (Conti.)

1. Create a table Employee.


2. Insert the values as mentioned.
3. Add column Employee ID as identity column.
4. Modify the column salary to store floating point values.
5. Rename the attribute FNAME to Emp_name.
6. Drop column LNAME from the employee table.
7. Add column Department with default value as ‘CSE’
8. Add an annual increment to the salary of an employee whose joining date is 19th September
2017.
9. Modify the department value of DILIP and VIJAY to ME
10. Delete the employees belonging to ME department

38
Thank you

39

You might also like