sql 101 for business experts and stakeholders

60
SQL Structured Query Language

Upload: ivan-stepaniuk

Post on 21-Feb-2017

256 views

Category:

Data & Analytics


3 download

TRANSCRIPT

Page 1: SQL 101 for business experts and stakeholders

SQLStructured Query Language

Page 2: SQL 101 for business experts and stakeholders

SQL

● Define the database

● Query data

● Manipulate data

● Add more data

Page 3: SQL 101 for business experts and stakeholders

Databaseapplicationsapplicationsapplications

reporting

administrationSQL

SQL

SQL

Page 4: SQL 101 for business experts and stakeholders

reporting

SQL

SQL

SQL

Page 5: SQL 101 for business experts and stakeholders

SQL 101

● Define the database

● Query data

● Manipulate data

● Add more data

Page 6: SQL 101 for business experts and stakeholders

Database vs. Spreadsheets

● Each table is like an Excel sheet

● Structure is much more important

● Calculations are not* part of the table

Page 7: SQL 101 for business experts and stakeholders

Tables

● Divided in rows or records● And divided in columns or fields

Columns describe the characteristics or quantities of what is described in each row, but never* the other way around.

Page 8: SQL 101 for business experts and stakeholders

Querying

Page 9: SQL 101 for business experts and stakeholders

vegetables

id name color stock_count

1 Apple Red 10

2 Tomato Red 2

3 Banana Yellow 4

4 Avocado Green 15

Page 10: SQL 101 for business experts and stakeholders

vegetables

id name color stock_count

1 Apple Red 10

2 Tomato Red 2

3 Banana Yellow 4

4 Avocado Green 15

Page 11: SQL 101 for business experts and stakeholders

Sentences and clauses

“Return observations on the following characteristics, from the following tables, where the following criteria is met, organized in the following way.”

Page 12: SQL 101 for business experts and stakeholders

The SELECT statement

“Return observations on the following characteristics, from the following tables, where the following criteria is met.”

Page 13: SQL 101 for business experts and stakeholders

The SELECT statement

“Return the name and color, from the vegetables table, where the stock count is more than 4.”

Page 14: SQL 101 for business experts and stakeholders

The SELECT statement

SELECT the name and color, FROM the vegetables table, WHERE the stock count is more than 4.

Page 15: SQL 101 for business experts and stakeholders

The SELECT statement

SELECT the name and color, FROM the vegetables tableWHERE the stock count is more than 4.

Page 16: SQL 101 for business experts and stakeholders

The SELECT statement

SELECT name, colorFROM vegetablesWHERE stock count > 4.

Page 17: SQL 101 for business experts and stakeholders

The SELECT statement

SELECT "name", "color"FROM "vegetables"WHERE "stock_count" > 4;

Page 18: SQL 101 for business experts and stakeholders

vegetables

id name color stock_count

1 Apple Red 10

2 Tomato Red 2

3 Banana Yellow 4

4 Avocado Green 15

Page 19: SQL 101 for business experts and stakeholders

Result

name color

Apple Red

Avocado Green

Page 20: SQL 101 for business experts and stakeholders

Combining expressions

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4 AND "color" = 'Red';

Page 21: SQL 101 for business experts and stakeholders

vegetables

id name color stock_count

1 Apple Red 10

2 Tomato Red 2

3 Banana Yellow 4

4 Avocado Green 15

Page 22: SQL 101 for business experts and stakeholders

Result

id color

1 Red

Page 23: SQL 101 for business experts and stakeholders

More combinations

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4

AND ("color" = 'Red' OR "name" = 'Pear');

Page 24: SQL 101 for business experts and stakeholders

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4

AND ("color" = 'Red' OR "name" = 'Pear') ;

More combinations

Page 25: SQL 101 for business experts and stakeholders

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4

AND ("color" = 'Red' OR "name" = 'Pear') ;

More combinations

Page 26: SQL 101 for business experts and stakeholders

More combinations

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4

AND ("color" = 'Red' OR "name" = 'Pear') ;

Page 27: SQL 101 for business experts and stakeholders

Result

id color

1 Red

Page 28: SQL 101 for business experts and stakeholders

Other comparisons● Column values can be compared to literals or to other

columns, using:○ =○ >○ <○ >=○ <=○ != ○ some specialized ones for dates, etc.

Page 29: SQL 101 for business experts and stakeholders

Sentences and clauses

“Return observations on the following characteristics, from the following tables, where the following criteria is met, organized in the following way.”

Page 30: SQL 101 for business experts and stakeholders

Ordering results

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4

ORDER BY

"id";

Page 31: SQL 101 for business experts and stakeholders

vegetables

id name color stock_count

1 Apple Red 10

2 Tomato Red 2

3 Banana Yellow 4

4 Avocado Green 15

Page 32: SQL 101 for business experts and stakeholders

Result

name color

Avocado Green

Apple Red

Page 33: SQL 101 for business experts and stakeholders

Limiting the result set

SELECT

* --Everything

FROM

"vegetables"

ORDER BY

"stock_count" DESC

LIMIT

2;

Page 34: SQL 101 for business experts and stakeholders

Result

id name color stock_count

4 Avocado Green 15

1 Apple Red 10

Page 35: SQL 101 for business experts and stakeholders

Top 10 SKUs by stock level

Chartio example

Page 36: SQL 101 for business experts and stakeholders

Write a query for a chart (table) that returns the name of the top 10 customers

by qty. of boxes, whose level is either gold or bronze.

DIY

Page 37: SQL 101 for business experts and stakeholders

Recap...● Identifiers go between double quotes, "color".

● Text goes between simple quotes, like 'Red'.

● Expressions combine with AND, OR, and NOT.

● We can ORDER BY any column, ASC or DESC.

● We can LIMIT the amount of rows we get.

Page 38: SQL 101 for business experts and stakeholders

More than one table

Joining

Page 39: SQL 101 for business experts and stakeholders

Sets, the cartesian product

Car

Truck

Van

Red

Green

Blue

color vehicle

Page 40: SQL 101 for business experts and stakeholders

Sets, the cartesian product

Car

Truck

Van

Red

Green

Blue

Red Car Red VanRed Truck Blue CarBlue Van Blue Truck...

color vehiclePRODUCT

Page 41: SQL 101 for business experts and stakeholders

Two tables

id name color

1 Jacket 2

2 Sweater 1

3 Shirt 1

id name

1 Red

2 Green

3 Blue

color item

Page 42: SQL 101 for business experts and stakeholders

Two tables

id name color

1 Jacket 2

2 Sweater 1

3 Shirt 1

id name

1 Red

2 Green

3 Blue

color item

Page 43: SQL 101 for business experts and stakeholders

Two tables

id name color

1 Jacket 2

2 Sweater 1

3 Shirt 1

id name

1 Red

2 Green

3 Blue

color item

Page 44: SQL 101 for business experts and stakeholders

SELECT

*

FROM

"item"

JOIN

"color";

The JOIN clause

Page 45: SQL 101 for business experts and stakeholders

Two tables product

id name id name color

1 Red 1 Jacket 2

1 Red 2 Sweater 1

1 Red 3 Shirt 1

2 Green 1 Jacket 2

2 Green 2 Sweater 1

2 Green 3 Shirt 1

3 Blue 1 Jacket 2

3 Blue 2 Sweater 1

3 Blue 3 Shirt 1

item X color

Page 46: SQL 101 for business experts and stakeholders

Two tables, also quite useless

id name color

1 Jacket 2

2 Sweater 1

3 Shirt 1

id name

1 Red

2 Green

3 Blue

color item

Page 47: SQL 101 for business experts and stakeholders

Two tables, product

id name id name color

1 Red 1 Jacket 2

1 Red 2 Sweater 1

1 Red 3 Shirt 1

2 Green 1 Jacket 2

2 Green 2 Sweater 1

2 Green 3 Shirt 1

3 Blue 1 Jacket 2

3 Blue 2 Sweater 1

3 Blue 3 Shirt 1

item X color

Page 48: SQL 101 for business experts and stakeholders

Two tables, restricted product

id name id name color

1 Red 1 Jacket 2

1 Red 2 Sweater 1

1 Red 3 Shirt 1

2 Green 1 Jacket 2

2 Green 2 Sweater 1

2 Green 3 Shirt 1

3 Blue 1 Jacket 2

3 Blue 2 Sweater 1

3 Blue 3 Shirt 1

item X color, only WHERE the color "id" equals "color"

Page 49: SQL 101 for business experts and stakeholders

Two tables, restricted productitem X color, only WHERE the color "id" equals "color"

id name id name color

1 Red 2 Sweater 1

1 Red 3 Shirt 1

2 Green 1 Jacket 2

Page 50: SQL 101 for business experts and stakeholders

Only some columnsitem X color product, only WHERE the color "id" equals the item "color",

SELECTing only some columns.

name name

Sweater Red

Shirt Red

Jacket Green

Page 51: SQL 101 for business experts and stakeholders

SELECT

"item"."name",

"color"."name"

FROM

"item"

JOIN

"color" ON "item"."color" = "color"."id";

The JOIN clause, ON

Page 52: SQL 101 for business experts and stakeholders

Name name what?

name name

Sweater Red

Shirt Red

Jacket Green

Page 53: SQL 101 for business experts and stakeholders

SELECT

"item"."name" AS "Item Name",

"color"."name" AS "Color"

FROM

"item"

JOIN

"color" ON "item"."color" = "color"."id";

Aliases, AS

Page 54: SQL 101 for business experts and stakeholders

Better

Item name Color

Sweater Red

Shirt Red

Jacket Green

Page 55: SQL 101 for business experts and stakeholders

SELECT

i."name" AS "Item Name",

c."name" AS "Color"

FROM

"item" AS i

JOIN

"color" AS c ON i."color" = c."id";

Aliases, AS also for tables

Page 56: SQL 101 for business experts and stakeholders

The personal shopper names of thelast 20 signups, without repeating.

Chartio example

Page 57: SQL 101 for business experts and stakeholders

Write a query for a chart (table) that returns the name of the top 10 customers

by qty. of boxes, whose level is either gold or bronze. Also include the name of

their PS.

DIY

Page 58: SQL 101 for business experts and stakeholders
Page 59: SQL 101 for business experts and stakeholders

Summing, counting, averaging multiple rows into one.

Next: Aggregation

Page 60: SQL 101 for business experts and stakeholders

Iván Stepaniuk@istepaniuk