SQL 101 for business experts and stakeholders

Preview:

Citation preview

SQLStructured Query Language

SQL

● Define the database

● Query data

● Manipulate data

● Add more data

Databaseapplicationsapplicationsapplications

reporting

administrationSQL

SQL

SQL

reporting

SQL

SQL

SQL

SQL 101

● Define the database

● Query data

● Manipulate data

● Add more data

Database vs. Spreadsheets

● Each table is like an Excel sheet

● Structure is much more important

● Calculations are not* part of the table

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.

Querying

vegetables

id name color stock_count

1 Apple Red 10

2 Tomato Red 2

3 Banana Yellow 4

4 Avocado Green 15

vegetables

id name color stock_count

1 Apple Red 10

2 Tomato Red 2

3 Banana Yellow 4

4 Avocado Green 15

Sentences and clauses

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

The SELECT statement

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

The SELECT statement

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

The SELECT statement

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

The SELECT statement

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

The SELECT statement

SELECT name, colorFROM vegetablesWHERE stock count > 4.

The SELECT statement

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

vegetables

id name color stock_count

1 Apple Red 10

2 Tomato Red 2

3 Banana Yellow 4

4 Avocado Green 15

Result

name color

Apple Red

Avocado Green

Combining expressions

SELECT

"id", "color"

FROM

"vegetables"

WHERE

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

vegetables

id name color stock_count

1 Apple Red 10

2 Tomato Red 2

3 Banana Yellow 4

4 Avocado Green 15

Result

id color

1 Red

More combinations

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4

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

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4

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

More combinations

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4

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

More combinations

More combinations

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4

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

Result

id color

1 Red

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

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

Sentences and clauses

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

Ordering results

SELECT

"id", "color"

FROM

"vegetables"

WHERE

"stock_count" > 4

ORDER BY

"id";

vegetables

id name color stock_count

1 Apple Red 10

2 Tomato Red 2

3 Banana Yellow 4

4 Avocado Green 15

Result

name color

Avocado Green

Apple Red

Limiting the result set

SELECT

* --Everything

FROM

"vegetables"

ORDER BY

"stock_count" DESC

LIMIT

2;

Result

id name color stock_count

4 Avocado Green 15

1 Apple Red 10

Top 10 SKUs by stock level

Chartio example

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

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.

More than one table

Joining

Sets, the cartesian product

Car

Truck

Van

Red

Green

Blue

color vehicle

Sets, the cartesian product

Car

Truck

Van

Red

Green

Blue

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

color vehiclePRODUCT

Two tables

id name color

1 Jacket 2

2 Sweater 1

3 Shirt 1

id name

1 Red

2 Green

3 Blue

color item

Two tables

id name color

1 Jacket 2

2 Sweater 1

3 Shirt 1

id name

1 Red

2 Green

3 Blue

color item

Two tables

id name color

1 Jacket 2

2 Sweater 1

3 Shirt 1

id name

1 Red

2 Green

3 Blue

color item

SELECT

*

FROM

"item"

JOIN

"color";

The JOIN clause

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

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

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

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"

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

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

SELECT

"item"."name",

"color"."name"

FROM

"item"

JOIN

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

The JOIN clause, ON

Name name what?

name name

Sweater Red

Shirt Red

Jacket Green

SELECT

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

"color"."name" AS "Color"

FROM

"item"

JOIN

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

Aliases, AS

Better

Item name Color

Sweater Red

Shirt Red

Jacket Green

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

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

Chartio example

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

Summing, counting, averaging multiple rows into one.

Next: Aggregation

Iván Stepaniuk@istepaniuk

Recommended