Writeups - Assignment 4
Writeups - Assignment 4
Title: Write SQL queries to demonstrate the use of concepts like join, sub-query and views.
Theory:
SQL JOIN
As the name shows, JOIN means to combine something. In case of SQL, JOIN means "to
combine two or more tables".
The SQL JOIN clause takes records from two or more tables in a database and combines it
together.
1. inner join,
5. cross join.
In the process of joining, rows of both tables are combined in a single table.
If you want to access more than one table through a select statement. If you want to combine two
or more table then SQL JOIN statement is used .it combines rows of that tables in one table and
one can retrieve the information by a SELECT statement. The joining of two or more tables is
based on common field between them. SQL INNER JOIN also known as simple join is the most
common type of join.
1.Staff table
2.Payment table
3 MONTY 25 2500
1 ARYAN 22 3000
4 AMIT 25 3500
1 ARYAN 22 3000
In the SQL outer JOIN all the content of the both tables are integrated together either they are
matched or not.
1.Left outer join (also known as left join): this join returns all the rows from left table combine
with the matching rows of the right table. If you get no matching in the right table it returns
NULL values.
2.Right outer join (also known as right join): this join returns all the rows from right table are
combined with the matching rows of left table .If you get no column matching in the left table .it
returns null value.
The SQL left join returns all the values from the left table and it also includes matching values
from right table, if there are no matching join value it returns NULL.
FROM table1
LEFTJOIN table2
ON table1.column_field = table2.column_field;
let us take two tables in this example to elaborate all the things:
CUSTOMER TABLE:
1 ARYAN 51 56000
2 AROHI 21 25000
3 VINEET 24 31000
4 AJEET 23 32000
5 RAVI 23 42000
ORDER TABLE:
FROM CUSTOMER
ON CUSTOMER.ID = ORDER.CUSTOMER_ID;
The SQL right join returns all the values from the rows of right table. It also includes the
matched values from left table but if there is no matching in both tables, it returns NULL.
FROM table1
ON table1.column_field = table2.column_field;
let us take an example with 2 tables table1 is CUSTOMERS table and table2 is ORDERS table.
CUSTOMER TABLE:
1 ARYAN 51 56000
2 AROHI 21 25000
3 VINEET 24 31000
4 AJEET 23 32000
5 RAVI 23 42000
ORDER TABLE:
Here we will join these two tables with SQL RIGHT JOIN:
FROM CUSTOMER
ON CUSTOMER.ID = ORDER.CUSTOMER_ID;
ID NAME AMOUNT DATE
The SQL full join is the result of combination of both left and right outer join and the join tables
have all the records from both tables. It puts NULL on the place of matches not found.
SQL full outer join and SQL join are same. generally it is known as SQL FULL JOIN.
SQL full outer join is used to combine the result of both left and right outer join and returns all
rows (don't care its matched or unmatched) from the both participating tables.
SELECT *
FROM table1
ON table1.column_name = table2.column_name;
Note:here table1 and table2 are the name of the tables participating in joining and column_name
is the column of the participating tables.
table_A
A M
1 m
2 n
4 o
table_B
A N
2 p
3 q
5 r
Resulting table
A M A N
2 n 2 p
1 m - -
4 o - -
- - 3 q
- - 5 r
Because this is a full outer join so all rows (both matching and non-matching) from both tables
are included in the output. Here only one row of output displays values in all columns because
there is only one match between table_A and table_B.
o Join operation in SQL is used to combine multiple tables together into a single table.
o If we use the cross join to combine two different tables, then we will get the Cartesian
product of the sets of rows from the joined table. When each row of the first table is
combined with each row from the second table, it is known as Cartesian join or cross
join.
o After performing the cross join operation, the total number of rows present in the final
table will be equal to the product of the number of rows present in table 1 and the number
of rows present in table 2.
o For example:
If there are two records in table 1 and three records in table 2, then after performing cross
join operation, we will get six records in the final table.
o Let us take a look at the syntax of writing a query to perform the cross join operation in
SQL.
Now let us see take a deeper dive into the cross join in SQL with the help of examples. All the
queries in the examples will be written using the MySQL database.
Table 1: MatchScore
Franklin 1 2
Alan 1 3
Priyanka 2 2
Rajesh 3 5
Table 2: Departments
Department_id Department_name
1 IT
2 HR
3 Marketing
Table 3: employee
Table 4: department
1 Production 1
2 Sales 3
3 Marketing 4
4 Accounts 5
Table 5: loan
1 B1 15000
2 B2 10000
3 B3 20000
4 B4 100000
Table 6: borrower
1 Sonakshi Dixit 1
2 Shital Garg 4
3 Swara Joshi 5
4 Isha Deshmukh 2
Table 7: customer
Table 8: orders
1 2012-01-20 2 3000
2 2012-05-18 2 2000
3 2012-06-28 3 4000
Example 1:
Write a query to perform the cross join operation considering the MatchScore table as the left
table and the Departments table as the right table.
Query:
We have used the SELECT command with the asterisk to retrieve all the columns present in the
MatchScore and Departments table. Then we have used the CROSS JOIN keyword to perform
the cross join operation on the MatchScore and Departments table. Since there are 4 records in
the MatchScore and 3 records in the Departments table, after performing the cross join operation,
we will get 12 rows.
After executing this query, you will find the following result:
Franklin 1 2 1 IT
Alan 1 3 1 IT
Priyanka 2 2 1 IT
Rajesh 3 5 1 IT
Franklin 1 2 2 HR
Alan 1 3 2 HR
Priyanka 2 2 2 HR
Rajesh 3 5 2 HR
Franklin 1 2 3 Marketing
Alan 1 3 3 Marketing
Priyanka 2 2 3 Marketing
Rajesh 3 5 3 Marketing
Each row from the MatchScore table is combined with each row of the Departments table. Since
there are four records in the MatchScore and three records in the Departments table, we have got
12 rows in the final table after performing the cross join operation.
Example 2:
Write a query to perform the cross join operation considering the employee table as the left table
and the department table as the right table.
Query:
We have used the SELECT command with the asterisk to retrieve all the columns present in the
employee and department table. Then we have used the CROSS JOIN keyword to perform the
cross join operation on the employee and department table. Since there are five records in the
employee and four records in the department table, after performing the cross join operation, we
will get 20 rows.
After executing this query, you will find the following result:
Each row from the employee table is combined with each row of the department table. Since
there are five records in the employee table and four records in the department table, we have got
20 rows in the final table after performing the cross join operation.
Example 3:
Write a query to perform the cross join operation considering the loan table as the left table and
the borrower table as the right table.
Query:
We have used the SELECT command with the asterisk to retrieve all the columns present in the
loan and the borrower table. Then we have used the CROSS JOIN keyword to perform the cross
join operation on the loan and the borrower table. Since there are four records in the loan table
and four records in the borrower table, after performing the cross join operation, we will get 16
rows.
After executing this query, you will find the following result:
LoanID Branch Amount CustID CustName LoanID
Example 4:
Write a query to perform the cross join operation considering the customer table as the left table
and the orders table as the right table.
Query:
We have used the SELECT command with the asterisk to retrieve all the columns present in the
customer and orders table. Then we have used the CROSS JOIN keyword to perform the cross
join operation on the customer table and the orders table. Since there are three records in the loan
table and three records in the orders table, after performing the cross join operation, we will get 9
rows.
After executing this query, you will find the following result:
Each row from the customers' table is combined with each row of the orders table. Since there
are three records in the loan table and three records in the orders table, after performing the cross
join operation, we will get 9 rows.
SQL View
SQL provides the concept of VIEW, which hides the complexity of the data and restricts
unnecessary access to the database. It permits the users to access only a particular column rather
than the whole data of the table.
The View in the Structured Query Language is considered as the virtual table, which depends on
the result-set of the predefined SQL statement.
Like the SQL tables, Views also store data in rows and columns, but the rows do not have any
physical existence in the database.
Any database administrator and user can easily create the View by selecting the columns from
one or more database tables. They can also delete and update the views according to their needs.
A view can store either all the records of the table or a particular record from the table using the
WHERE clause.
3. FROM Table_Name
4. WHERE condition;
In the syntax, View_Name is the name of View you want to create in SQL. The SELECT
command specifies the rows and columns of the table, and the WHERE clause is optional, which
is used to select the particular record from the table.
You can create a View from multiple tables by including the tables in the SELECT statement.
4. WHERE condition;
Table: Student_Details
Suppose, you want to create a view with Stu_ID, Stu_Subject, and Stu_Marks of those students
whose marks are greater than 85. For this issue, you have to type the following query:
3. FROM Student_Details
Output:
1001 Math 85
1003 Math 87
1004 English 95
1005 Hindi 99
1006 Computer 90
View: Student_View
Let's consider two tables, Student_Details and Teacher_Details. The Student_Details table
consists of Stu_ID, Stu_Name, Stu_Subject, and Stu_Marks columns. And, the Teacher_Details
table consists of Teacher_ID, Teacher_Name, Teacher_Subject, Teacher_City columns. The data
of the Student_Details and Teacher_Details is shown in the following two tables:
Table: Student_Details
Table: Teacher_Details
Suppose, you want to create a view with Stu_ID, Stu_Name, Teacher_ID, and Teacher_Subject
columns from the Student_Details and Teacher_Details tables.
1. CREATE VIEW Student_Teacher_View AS
To display the data of Student_Teacher_View, you have to type the following SELECT query:
Output:
View: Student_Teacher_View
We can also modify existing data and insert the new record into the view in the Structured Query
Language. A view in SQL can only be modified if the view follows the following conditions:
1. You can update that view which depends on only one table. SQL will not allow updating
the view which is created more than one table.
2. The fields of view should not contain NULL values.
3. The view does not contain any subquery and DISTINCT keyword in its definition.
4. The views cannot be updatable if the SELECT statement used to create a View contains
JOIN or HAVING or GROUP BY clause.
5. If any field of view contains any SQL aggregate function, you cannot modify the view.
Syntax to Update a View
FROM Table_Name
WHERE condition;
If we want to update the above Student_View and add the Stu_Name attribute from the Student
table in the view, you have to type the following Replace query in SQL:
FROM Student_Details
The above statement updates the existing Student_View and updates the data based on the
SELECT statement.
Now, you can see the virtual table by typing the following query:
Output:
View: Student_View
Insert the new row into the existing view
Just like the insertion process of database tables, we can also insert the record in the views. The
following SQL INSERT statement is used to insert the new row or record in the view:
Suppose, you want to insert the record of a new student in the Student_View, then you have to
write the following query in SQL:
Now, you can check that the new record is inserted in the Student_View or not by using the
following query:
Output:
1001 Math 85
1003 Math 87
1004 English 95
1005 Hindi 99
1006 Computer 90
1007 Hindi 89
View: Student_View
Delete the existing row from the view
Just like the deletion process of database tables, we can also delete the record from the views.
The following SQL DELETE statement is used to delete the existing row or record from the
view:
Suppose, you want to delete the record of those students from the Student_View whose Subject
is Math, then you have to type the following SQL query:
Now, you can check that the record is removed from the Student_View or not by using the
following query:
Output:
1004 English 95
1005 Hindi 99
1006 Computer 90
1007 Hindi 89
View: Student_View
Drop a View
We can also delete the existing view from the database if it is no longer needed. The following
SQL DROP statement is used to delete the view:
A Subquery is a query within another SQL query and embedded within the WHERE clause.
Important Rule:
o A subquery can be placed in a number of SQL clauses like WHERE clause, FROM
clause, HAVING clause.
o You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
o A subquery is a query within another query. The outer query is known as the main query,
and the inner query is known as a subquery.
o In the Subquery, ORDER BY command cannot be used. But GROUP BY command can
be used to perform the same function as ORDER BY command.
SQL subqueries are most frequently used with the Select statement.
Syntax
SELECT column_name
FROM table_name
Example
1 John 20 US 2000.00
4 Alina 29 UK 6500.00
SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
4 Alina 29 UK 6500.00
o SQL subquery can also be used with the Insert statement. In the insert statement, data
returned from the subquery is used to insert into another table.
o In the subquery, the selected data can be modified with any of the character, date
functions.
Syntax:
SELECT *
FROM table_name
Example
Now use the following syntax to copy the complete EMPLOYEE table into the
EMPLOYEE_BKP table.
WHERE ID IN (SELECT ID
FROM EMPLOYEE);
The subquery of SQL can be used in conjunction with the Update statement. When a subquery is
used with the Update statement, then either single or multiple columns in a table can be updated.
Syntax
UPDATE table
(SELECT COLUMN_NAME
FROM TABLE_NAME
WHERE condition);
Example
UPDATE EMPLOYEE
This would impact three rows, and finally, the EMPLOYEE table would have the following
records.
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
The subquery of SQL can be used in conjunction with the Delete statement just like any other
statements mentioned above.
Syntax
(SELECT COLUMN_NAME
FROM TABLE_NAME
WHERE condition);
Example
This would impact three rows, and finally, the EMPLOYEE table would have the following
records.
1 John 20 US 2000.00