joins - anjalideorecom.files.wordpress.com€¦  · web viewassignment no.4. title of assignment:...

14
Assignment No.4 Title of Assignment:Design at least 10 SQL/NoSQL queries for suitable database application using SQL DML statements: all types of Join, Sub- Query and View. Objective:to understand all types of Joins in SQL, Sub queries, operations on views such as insert, update, delete Theory : Joins The SQL Joins clause is used to combine records from two or more tables in a database. A JOIN is a means for combining fields from two tables by using values common to each. There are different types of joins available in SQL − INNER JOIN (SIMPLE JOIN) − returns rows when there is a match in both tables. LEFT JOIN − returns all rows from the left table, even if there are no matches in the right table. RIGHT JOIN − returns all rows from the right table, even if there are no matches in the left table. FULL JOIN − returns rows when there is a match in one of the tables. SELF JOIN − is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement. CARTESIAN JOIN − returns the Cartesian product of the sets of records from the two or more joined tables.

Upload: others

Post on 02-Jan-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

Assignment No.4Title of Assignment:Design at least 10 SQL/NoSQL queries for suitable database application using SQL DML statements: all types of Join, Sub-Query and View.

Objective:to understand all types of Joins in SQL, Sub queries, operations on views such as insert, update, delete

Theory :

Joins

The SQL Joins clause is used to combine records from two or more tables in a database. A JOIN is a means for combining fields from two tables by using values common to each.

There are different types of joins available in SQL −

INNER JOIN (SIMPLE JOIN) − returns rows when there is a match in both tables.

LEFT JOIN − returns all rows from the left table, even if there are no matches in the right table.

RIGHT JOIN − returns all rows from the right table, even if there are no matches in the left table.

FULL JOIN − returns rows when there is a match in one of the tables.

SELF JOIN − is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement.

CARTESIAN JOIN − returns the Cartesian product of the sets of records from the two or more joined tables.

Page 2: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

CARTESIAN or CROSS JOINSThe CARTESIAN JOIN or CROSS JOIN returns the Cartesian product of the sets of

records from two or more joined tables. Thus, it equates to an inner join where the join-condition always evaluates to either True or where the join-condition is absent from the statement.

SyntaxThe basic syntax of the CARTESIAN JOIN or the CROSS JOIN is as follows −

SELECT table1.column1, table2.column2...

FROM table1, table2 [, table3 ]

INNER JOINS The most important and frequently used of the joins is the INNER JOIN.

They are also referred to as an EQUIJOIN.

Page 3: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

The INNER JOIN creates a new result table by combining column values of two tables (table1 and

table2) based upon the join-predicate.

The query compares each row of table1 with each row of table2 to find all pairs of rows which

satisfy the join-predicate.

When the join-predicate is satisfied, column values for each matched pair of rows of A and B are

combined into a result row.

SyntaxThe basic syntax of the INNER JOIN is as follows.

SELECT table1.column1, table2.column2...

FROM table1

INNER JOIN table2

ON table1.common_field = table2.common_field;

LEFT OUTER JOINSThe SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table.This means that a left join returns all the values from the left table, plus matched values from the right table or NULL in case of no matching join predicate.

SyntaxThe basic syntax of a LEFT JOIN is as follows.

SELECT columns

FROM table1

LEFT [OUTER] JOIN table2

ON table1.column = table2.column;

Here, the given condition could be any given expression based on your requirement.

Page 4: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

RIGHT OUTER JOINSThe SQL RIGHT JOIN returns all rows from the right table, even if there are no

matches in the left table. This means that if the ON clause matches 0 (zero) records in the left table; the join will still return a row in the result, but with NULL in each column from the left table.

This means that a right join returns all the values from the right table, plus matched values from the left table or NULL in case of no matching join predicate.

SyntaxThe basic syntax of a RIGHT JOIN is as follow.

SELECT table1.column1, table2.column2...

FROM table1

RIGHT JOIN [OUTER] table2

ON table1.common_field = table2.common_field;

FULL OUTER JOINSThe SQL FULL JOIN combines the results of both left and right outer joins.The joined table will contain all records from both the tables and fill in NULLs for missing matches on either side.

Syntax

The basic syntax of a FULL JOIN is as follows −

SELECT table1.column1, table2.column2...

FROM table1

FULL JOIN [OUTER] table2

ON table1.common_field = table2.common_field;

Here, the given condition could be any given expression based on your requirement.

SELF JOINSThe SQL SELF JOIN is used to join a table to itself as if the table were two

tables; temporarily renaming at least one table in the SQL statement.

Syntax

Page 5: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

The basic syntax of SELF JOIN is as follows −

SELECT a.column_name, b.column_name...

FROM table1 a, table1 b

WHERE a.common_field = b.common_field;

Here, the WHERE clause could be any given expression based on your requirement.

Example

Cross JOIN

SELECT * FROM class CROSS JOIN class_info;

INNER Join or EQUI Join

SELECT * from class INNER JOIN class_info where class.id = class_info.id;

Apples Table

Variety Price

Page 6: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

Fuji 5.00

Gala 6.00

Oranges Table

Variety Price

Valencia 4.00

Navel 5.00

Left join:select * from apples as a

left outer join oranges as o on a.price = o.price

variety price variety price

Fuji 5 Navel 5

Gala 6 NULL NULL

Right join:

select * from apples as a

right outer join oranges as o on a.price = o.price

variety price variety price

NULL NULL Valencia 4

Fuji 5 Navel 5

FULL OUTER JOIN:

variety price variety price

Fuji 5 Navel 5

Page 7: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

Gala 6 NULL NULL

NULL NULL Valencia 4

subqueryA subquery is a SQL query nested inside a larger query.

A subquery may occur in : - A SELECT clause - A FROM clause - A WHERE clause

The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery.

A subquery is usually added within the WHERE Clause of another SQL SELECT statement. You can use the comparison operators, such as >, <, or =. The comparison operator can also be a

multiple-row operator, such as IN, ANY, or ALL. A subquery is also called an inner query or inner select, while the statement containing a

subquery is also called an outer query or outer select. The inner query executes first before its parent query so that the results of an inner query can be

passed to the outer query.

You can use a subquery in a SELECT, INSERT, DELETE, or UPDATE statement to perform the following tasks:

Compare an expression to the result of the query. Determine if an expression is included in the results of the query. Check whether the query selects any rows.

Syntax

The subquery (inner query) executes once before the main query (outer query) executes.

Page 8: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

The main query (outer query) use the subquery result.

Subqueries: Guidelines There are some guidelines to consider when using subqueries :

A subquery must be enclosed in parentheses. A subquery must be placed on the right side of the comparison operator. Subqueries cannot manipulate their results internally, therefore ORDER BY clause cannot be

added into a subquery. You can use an ORDER BY clause in the main SELECT statement (outer query) which will be the last clause.

Use single-row operators with single-row subqueries. If a subquery (inner query) returns a null value to the outer query, the outer query will not return

any rows when using certain comparison operators in a WHERE clause.

Type of Subqueries Single row subquery : Returns zero or one row. Multiple row subquery : Returns one or more rows. Multiple column subqueries : Returns one or more columns. Correlated subqueries : Reference one or more columns in the outer SQL statement. The

subquery is known as a correlated subquery because the subquery is related to the outer SQL statement.

Nested subqueries : Subqueries are placed within another subquery.

Subqueries with INSERT statement

INSERT statement can be used with subqueries. Here are the syntax and an example of subqueries using INSERT statement.

Syntax:

INSERT INTO table_name [ (column1 [, column2 ]) ]

SELECT [ *|column1 [, column2 ]

FROM table1 [, table2 ]

[ WHERE VALUE OPERATOR ];

Example:

INSERT INTO neworder

SELECT * FROM orders

WHERE advance_amount in(2000,5000);

Page 9: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

Subqueries with UPDATE statement

In a UPDATE statement, you can set new column value equal to the result returned by a single row subquery. Here are the syntax and an example of subqueries using UPDATE statement.Syntax:

UPDATE table SET column_name = new_value

[ WHERE OPERATOR [ VALUE ]

(SELECT COLUMN_NAME

FROM TABLE_NAME)

[ WHERE) ]

Example:

UPDATE neworder

SET ord_date='15-JAN-10'

WHERE ord_amount-advance_amount< (SELECT MIN(ord_amount) FROM orders);

Subqueries with DELETE statement

DELETE statement can be used with subqueries. Here are the syntax and an example of subqueries using DELETE statement.

Syntax:

DELETE FROM TABLE_NAME

[ WHERE OPERATOR [ VALUE ]

(SELECT COLUMN_NAME

FROM TABLE_NAME)

[ WHERE) ]

Example:

DELETE FROM neworder

WHERE advance_amount<(SELECT MAX(advance_amount) FROM orders);

Page 10: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

Single Row Subqueries

A single row subquery returns zero or one row to the outer SQL statement. You can place a subquery in a WHERE clause, a HAVING clause, or a FROM clause of a SELECT statement.

Single-Row Subqueries Return only one row Use single-row comparison operators

Multiple-Row SubqueriesSubqueries that return more than one row are called multiple-row subqueries. You use a multiple-row operator, instead of a single-row operator, with a multiple-

rowsubquery

The multiple-row operator expects one or

more values.ANY Operator in Multiple-Row Subqueries

ANY Operator in Multiple-Row Subqueries The ANY operator compares a value to each value returned by a subquery.

The slide example displays employees who are not IT programmers and whosesalary is less than that of any IT programmer. The maximum salary that aprogrammer earns is $9,000.

<ANY means less than the maximum.

Page 11: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

>ANY means greater than the minimum. =ANY is equivalent to IN. <ALL means less than the minimum. >ALL means greater than the maximum. When using SOME or ANY, you often use the DISTINCT keyword to prevent rows from being selected several times.

When using SOME or ANY, you often use the DISTINCT keyword to preventrows from being selected several times.

The NOT operator can be used with IN, ANY, and ALL operators.

Null Values in a SubqueryOne of the values returned by the inner query is a null value, and hence theentire query returns no rows. The reason is that all conditions that compare a null value result in anull. So whenever null values are likely to be part of the results set of a subquery,do not use the NOT IN operator.

Conclusion:

Page 12: Joins - anjalideorecom.files.wordpress.com€¦  · Web viewAssignment No.4. Title of Assignment: Design at least 10 SQL/NoSQL queries for suitable database application using SQL

We have explained the MySQL LEFT JOIN, RIGHT JOIN clause and shown you how to apply it to query data from multiple database tables.

We have shown you how to use MySQL subquery to write more complex queries.

We have shown you how to create an updateable view and how to update data in the underlying table through the view.

We have learned how to manage views in MySQL including displaying, modifying and removing views.