MySQL JOIN & UNION

17
MySQL JOIN & UNION Jamshid Hashimi Trainer, Cresco Solution http://www.jamshidhashimi.com [email protected] @jamshidhashimi ajamshidhashimi Afghanistan Workforce Development Program

Transcript of MySQL JOIN & UNION

Page 1: MySQL JOIN & UNION

MySQL JOIN & UNION

Jamshid HashimiTrainer, Cresco Solution

http://www.jamshidhashimi.com [email protected] @jamshidhashimi ajamshidhashimi

Afghanistan Workforce Development Program

Page 2: MySQL JOIN & UNION

Agenda

• Subqueries• Table Aliases• Multi-table Joins• UNION ALL• UNION Rules• GROUP BY

Page 3: MySQL JOIN & UNION

Subquery• A subquery is a SELECT statement within another statement.

• They allow queries that are structured so that it is possible to isolate each part of a statement.

• They provide alternative ways to perform operations that would otherwise require complex joins and unions.

• Many people find subqueries more readable than complex joins or unions. Indeed, it was the innovation of subqueries that gave people the original idea of calling the early SQL “Structured Query Language.”

SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);

Page 4: MySQL JOIN & UNION

Subquery

• A subquery can return a scalar (a single value), a single row, a single column, or a table (one or more rows of one or more columns).

• In MySQL, you cannot modify a table and select from the same table in a subquery. This applies to statements such as DELETE, INSERT, REPLACE, UPDATE.

Page 5: MySQL JOIN & UNION

Subquery

• EXISTS or NOT EXISTS– If a subquery returns any rows at all, EXISTS

subquery is TRUE, and NOT EXISTS subquery is FALSE.

SELECT column1 FROM t1 WHERE EXISTS (SELECT * FROM t2);

Page 6: MySQL JOIN & UNION

Table Aliases

• SQL aliases are used to give a database table, or a column in a table, a temporarily name.

• Basically aliases are created to make column names more readable.

SELECT column_name AS alias_nameFROM table_name;

Page 7: MySQL JOIN & UNION

MySQL JOIN

• “JOIN” is an SQL keyword used to query data from two or more related tables.

• The act of joining in MySQL refers to smashing two or more tables into a single table.

Page 8: MySQL JOIN & UNION

INNER JOIN

• The most frequently used clause is INNER JOIN. This produces a set of records which match in both the joined tables, i.e. all users who are enrolled on a course:

SELECT user.name, user.courseFROM userINNER JOIN course ON course.id=user.course;

Page 9: MySQL JOIN & UNION

LEFT JOIN

• If we do a LEFT JOIN, we get all records that match in the same way and IN ADDITION we get an extra record for each unmatched record in the left table of the join.

SELECT user.name, user.courseFROM userLEFT JOIN course ON course.id=user.course;

Page 10: MySQL JOIN & UNION

RIGHT JOIN

• A RIGHT JOIN produces a set of records which matches every entry in the right table regardless of any matching entry in the left table.

SELECT user.name, course.nameFROM userRIGHT JOIN course ON course.id=user.course;

Page 11: MySQL JOIN & UNION

UNION

• You can use UNION if you want to select rows one after the other from several tables, or several sets of rows from a single table all as a single result set.

• Syntax

SELECT ...UNION [ALL | DISTINCT] SELECT ...[UNION [ALL | DISTINCT] SELECT ...]

Page 12: MySQL JOIN & UNION

UNION

• MySQL uses the names of columns in the first SELECT statement as the labels for the output.

SELECT fname, lname, addr FROM prospectUNIONSELECT first_name, last_name, address FROM customerUNIONSELECT company, '', street FROM vendor;

Page 13: MySQL JOIN & UNION

UNION

• MySQL UNION with ORDER BY– If you want to sort the results returned from the

query using the UNION operator, you need to use ORDER BY clause in the last SQL SELECT statement. You can put each SELECT statement in the parentheses and use the ORDER BY clause as the last statement.

(SELECT customerNumber id,contactLastname nameFROM customers)UNION(SELECT employeeNumber id,firstname nameFROM employees)ORDER BY name,id

Page 14: MySQL JOIN & UNION

UNION Rules

• The number and the order of the columns must be the same in all queries.

• The data types must be compatible.• The columns selected in the different SELECT

statements must be in the same order

Page 15: MySQL JOIN & UNION

GROUP BY

• The most common types of aggregate functions let you find out things like the minimum, maximum and even the average of a "grouped" set of data. The trick to understanding aggregate functions is often understanding what kind of data is being grouped and analyzed.

SELECT type, MIN(price) FROM products GROUP BY type

Page 16: MySQL JOIN & UNION

GROUP BY

• Group BY is good for retrieving information about a group of data. If you only had one product of each type, then GROUP BY would not be all that useful.

• GROUP BY only shines when you have many similar things. For example, if you have a number of products of the same type, and you want to find out some statistical information like the minimum, maximum, or other top-level info, you would use GROUP BY.

Page 17: MySQL JOIN & UNION

QUESTIONS?