DBMS3
DBMS3
Practical No: 3
Title: SQL Queries - all types of join, Sub-Query and View.
Date of completion: 13th July 2024
Objectives: To develop Database programming skills.
Problem Statement:
SQL Queries - all types of Join, Sub-Query and View.
Write at least 10 SQL queries for suitable database application using SQL DML statements.
Software and Hardware requirements:
Theory:
➢ SQL JOIN
SQL JOIN clause is used to query and access data from multiple tables by establishing logical
relationships between them. It can access data from multiple tables simultaneously using common
key values shared across different tables.
We can use SQL JOIN with multiple tables. It can also be paired with other clauses; the most
popular use will be using JOIN with WHERE clause to filter data retrieval.
➢ Types of JOIN in SQL
There are many types of Joins in SQL. Depending on the use case, you can use different type of
SQL JOIN clause. Here are the frequently used SQL JOIN types:
1. SQL INNER 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.
Syntax:
The syntax for SQL INNER JOIN is:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Here,
• table1: First table.
DBMSLAB 23010086
• table2: Second table
• matching_column: Column common to both the tables.
LEFT JOIN returns all the rows of the table on the left side of the join and matches rows for
the table on the right side of the join. For the rows for which there is no matching row on the
right side, the result-set will contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
Syntax
The syntax of LEFT JOIN in SQL is:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Here,
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
DBMSLAB 23010086
3. SQL RIGHT JOIN
RIGHT 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.It is very similar to LEFT 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.
Syntax:
The syntax of RIGHT JOIN in SQL is:
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Here,
• table1: First table.
• table2: Second table
• matching_column: Column common to both the tables.
➢ SQL Subqueries
In SQL a Subquery can be simply defined as a query within another query. In other words we
can say that a Subquery is a query that is embedded in WHERE clause of another SQL query.
Important rules for Subqueries:
• You can place the Subquery in a number of SQL clauses: WHERE clause, HAVING clause,
FROM clause. Subqueries can be used with SELECT, UPDATE, INSERT, DELETE
statements along with expression operator. It could be equality operator or comparison
operator such as =, >, =, <= and Like operator.
• ORDER BY command cannot be used in a Subquery. GROUPBY command can be used to
perform same function as ORDER BY command.
• Use single-row operators with singlerow Subqueries. Use multiple-row operators with
multiple-row Subqueries.
Syntax: There is not any general syntax for Subqueries. However, Subqueries are seen
to be used most frequently with SELECT statement as shown below:
SELECT column_name
FROM table_name
WHERE column_name expression operator
(SELECT COLUMN_NAME from TABLE_NAME WHERE ... );
➢ SQL Views
Views in SQL are a kind of virtual table. A view also has rows and columns like tables, but a
view doesn’t store data on the disk like a table. View defines a customized query that
retrieves data from one or more tables, and represents the data as if it was coming from a
single source.
We can create a view by selecting fields from one or more tables present in the database. A
View can either have all the rows of a table or specific rows based on certain conditions.
DBMSLAB 23010086
• CREATE VIEWS in SQL
We can create a view using CREATE VIEW statement. A View can be created from a single
table or multiple tables.
Syntax
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE condition;
Parameters:
• view_name: Name for the View
• table_name: Name of the table
• condition: Condition to select rows
• DELETE VIEWS in SQL
SQL allows us to delete an existing View. We can delete or drop View using the DROP
statement.
Syntax
DROP VIEW view_name;
• UPDATE VIEW in SQL
If you want to update the existing data within the view, use the UPDATE statement.
Syntax
UPDATE view_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
If you want to update the view definition without affecting the data, use the CREATE OR
REPLACE VIEW statement. you can use this syntax:
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
CONCLUSION:
Understanding and effectively using SQL joins, subqueries, and views are essential for querying and
manipulating data in relational databases. Each technique serves distinct purposes: joins combine
data from multiple tables, subqueries provide nested queries for complex conditions or data
retrieval, and views offer a way to encapsulate and reuse query logic. Mastering these SQL features
enables efficient data manipulation and retrieval in database applications.
DBMSLAB 23010086
• Execution of Query:
1. Inner Join:
DBMSLAB 23010086
2. Left Join:
3. Right Join
DBMSLAB 23010086
4. Full Join
5. Sub-Queries:
DBMSLAB 23010086
8. Update name of the students to Sharvi in Student2 table whose city is same as Krutika,
Gayatri in Student table.
DBMSLAB 23010086
9. Creating view