Monday, September 8, 2014

Union, Intersect and Except



SELECT title FROM job_current
UNION
SELECT title FROM job_desired
UNION
SELECT title FROM job_listings;

Rules

1. UNION can only take one ORDER BY at the end of the statement.

SELECT title FROM job_current
UNION
SELECT title FROM job_desired
UNION
SELECT title FROM job_listings
ORDER BY title;

2. The number of columns in each SELECT must match.
3. Must also have the same expressions and aggregate functions in each SELECT statement.
4. SQL suppresses duplicate values from the results of a union.

UNION ALL

same as UNION but does not suppress duplicates

SELECT title FROM job_current
UNION ALL
SELECT title FROM job_desired
UNION ALL
SELECT title FROM job_listings
ORDER BY title;

Creating a table from your union

CREATE TABLE my_union AS
SELECT title FROM job_current UNION
SELECT title FROM job_desired
UNION SELECT title FROM job_listings

INTERSECT

To find parts of queries that overlap.

SELECT title FROM job_current
INTERSECT
SELECT title FROM job_desired;


EXCEPT

EXCEPT returns only those columns that are in the first query, but not in the second query.

SELECT title FROM job_current
EXCEPT
SELECT title FROM job_desired;


Outer join

An outer join returns all rows from one of the tables, along with matching information from another table.

With an inner join, you are comparing rows from two tables, but the order of those two tables does not matter.

Difference between an outer join and an inner join

An outer join gives a row whether there's a match with the other table or not.

A NULL value in the results of a left outer join means that the right table has no values that correspond to the left table.

Sunday, September 7, 2014

Subquery

A subquery is a query that is wrapped within another query. It's also called an INNER query.

SELECT mc.first_name, mc.last_name, mc.phone, jc.title
FROM job_current AS jc NATURAL JOIN my_contacts AS mc
WHERE jc.title IN (SELECT title FROM job_listings);

SELECT some_column, another_column
FROM table
WHERE column = (SELECT column FROM table)

Correlated subqueries

In a non correlated subquery the inner query, or subquery, gets interpreted by the RDBMS followed by the outer query.

SELECT mc.first_

Cartesian Join

The CROSS JOIN returns every row from one table crossed with every row from the second.

It is also called Cartesian Product, cross product, cross join or no join.

SELECT t.toy, b.boy
FROM toys AS t
CROSS JOIN
boys AS b;

or

SELECT toys.toy, boys.boy FROM toys, boys




Inner Join

An inner join is a cross join with some result rows removed by a condition in the query.
An inner join combines the records from two tables using comparison operators in a condition.

SELECT <some columns>
FROM table1
INNER JOIN
table2
ON <some conditions>

SELECT mc.last_name, mc.first_name, p.profession
FROM my_contacts AS mc
INNER JOIN
profession AS p
ON mc.contact_id = p.prof_id;

Equijoin

Inner joins test for equality

SELECT boys.boy, toys.toy
FROM boys
INNER JOIN
toys
ON boys.toy_id = toys.toy_id;

Non-Equijoin

Inner joins test for inequality.

SELECT boys.boy, toys.toy
FROM boys
INNER JOIN
toys
ON boys.toy_id <> toys.toy_id
ORDER BY boys.boy;

Natural Join

Natural join only work if the column you are joining by has the same name in both tables.

Natural join will recognize the same column name in each table and return matching rows.

SELECT boys.boy, toys.toy
FROM boys
NATURAL JOIN
toys;

Foreign Key



The foreign key is a column in a table that references the primary key of another table.


A NULL foreign key means that there is no matching primary key in the parent table.

Referential Integrity

You will only be able to insert values into your foreign key that exist in the table the key came from, the parent table.


You can use foreign key to reference a unique value in the parent table.
It does not have to be the primary key of the parent table, but it must be unique.

One-to-One: exactly one row of a parent table is related to one row of a child table.
It is not used that often.

One-to-many: a record in a table A can have many matching records in an other table B, but record in table B can only match ONE record in table A.

Many-to-Many: needs a junction table to avoid duplicates
It is usually made up of two one-to-many relationships with a junction table in between




Functions

RIGHT/LEFT

//start at the right side of location and get 2 characters
SELECT RIGHT(location, 2) FROM my_contacts

SELECT LEFT(location, 2) FROM my_contacts

SUBSTRING_INDEX

SELECT SUBSTRING_INDEX(location, ',', 1) FROM my_contacts
Finds everything in front of a specific character or string
If 1 were to be 2, then the second occurrence of the , would be looked and everything until that would be returned

SUM

SELECT SUM(sales)
FROM cookie_sales
WHERE first_name = 'Keith';

Normalization



To make tables completely normal, we need to give each record a primary key.

A primary key is a column in your table that makes each record unique. 





First Normal Form

Rule1: Columns contain only atomic values
Rule 2: No repeating groups of data





Composite key

A composite key is a primary key composed of multiple columns, creating a unique key.