Sei sulla pagina 1di 19

Ge#ng

your hands dirty


with SQL
24.1.2017
Aalto University
We use a nice demo sandbox provided
by a random company
•  hEp://www.dofactory.com/sql/sandbox

•  You can open this already now, we’ll get into


it soon
What is SQL?
•  Structured Query Language
•  The basic language computers and humans
use to chat with relaOonal databases

•  It can be used to…


–  Retrieve data
–  Insert new data
–  Delete or update data
Ok sure, but what is a
rela%onal database?

Interrelated tables with


defined fields (columns)
and records (lines)
Are all databases relaOonal?
•  No, there are many alternaOves. However, relaOonal
databases are the most common and they all use SQL

•  The most recent trend are large distributed databases,


helpfully called “NoSQL databases”
–  OpOmized for handling items, not fields
–  SQL can easily process individual field values for millions of
items, but do badly in processing enOre records
–  NoSQL is fast with millions of items, but (relaOvely) bad at
retrieving and processing specific field values for large
number of items
NoSQL items at Amazon DynamoDB

ID: 101 ID: 102


{ {
Title = "Book 101 Title" Title = "18-Bicycle 201"
ISBN = "111-1111111111" Description = "201 description"
Authors = "Author 1" BicycleType = "Road"
Price = "-2" Brand = "Brand-Company A"
Dimensions = "8.5 x 11.0 x 0.5" Price = "100"
PageCount = "500" Color = [ "Red", "Black" ]
InPublication = true ProductCategory = "Bike"
ProductCategory = "Book" }
}
Let’s get to the sandbox
•  hEp://www.dofactory.com/sql/sandbox

We will focus solely on SELECT command, which


retrieves data from the database


SELECT !
•  Let’s try the first one…
“SELECT id, firstname, lastname, city FROM
customer”

Does this work for you? .. Ok, try:

SELECT id, firstname, lastname, city FROM
customer ORDER BY city
Not so
simple, eh!

We will look
into
DISTINCT,
WHERE, and
GROUP BY

Let’s try to see all the ciOes we have
customers in?
SELECT city, country FROM customer

Is there something not ok with this?

If only we could get DISTINCT results?
Let’s see customers in Germany
SELECT id, firstname, lastname, city FROM
customer WHERE country = ‘Germany’

(SQL databases differ in specific, here Germany
is not case-sensiOve)

Recent orders?
SELECT * FROM [order]
WHERE year(orderdate) > 2013

Ok, this is REALLY annoying. Because order is a
command, we need to write order as a table
name in brackers – like [order]
Ge#ng more complexity…
•  How do we show ciOes where customers have
recently ordered from? => We JOIN two
tables.
SELECT DISTINCT city, country FROM customer,
[order]
WHERE customer.id = [order].customerId AND
year(orderdate) > 2013

(there are alternaOve ways to do this!)
Lets start aggregaOng…
•  Ge#ng averages, counts, min and max is easy
•  You need to GROUP BY the selects

CounOng customers per country


SELECT country, count(id) AS customers FROM
customer GROUP BY country
⇒ selects the name of countries and the counts of
id’s (grouped by country)
⇒ Note, that we labeled the count of id’s as
“customers”
We can sort countries…
•  SELECT country, count(id) AS customers FROM
customer GROUP BY country ORDER BY
customers DESC

•  ORDER BY x can be followed by ASC or DESC


(ascending or descending)
–  We have the most customers in USA & France
What is the average price of products
per each supplier?
SELECT supplier.companyname, avg(unitprice)
as avgprice FROM product, supplier WHERE
product.supplierId = supplier.id GROUP BY
supplier.companyname

Try: List product names & prices along with the
average number quanOty customers order
Some stats..
•  Average order volume per country
SELECT country, avg(totalamount) FROM
[order], customer WHERE customerid =
customer.id GROUP BY country

Try: List the total monetary value of all orders


for each customer
Tip: Use “sum(totalamount)”
Find product sold per country
select country, productname from customer, [order], product,
orderitem
where customer.id = [order].customerid and orderitem.producHd
= product.id and [order].id = orderitem.orderid
Cheats
select firstname, lastname, sum(totalamount) as
spending from customer, [order] where
customer.id = customerid group by firstname,
lastname order by spending
SELECT productname, product.unitprice,
avg(quanOty) as avgquanOty FROM orderItem,
product WHERE product.id = producOd GROUP
BY productname, product.unitprice

Potrebbero piacerti anche