exercise 04 sql i -  · exercise 04 –sql i. systems ... tpch database schema 10 exercise 3...

13
| | Systems Group Data Modelling and Databases FS 2018 22.03.2018 1 Exercise 04 SQL I

Upload: others

Post on 16-Jun-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

Data Modelling and Databases FS 2018

22.03.2018 1

Exercise 04 – SQL I

Page 2: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

Employee database schema

departments(dept_no:string, dept_name:string)

dept_emp(emp_no:int, dept_no:string, from_date:date, to_date:date)

dept_manager(emp_no:int, dept_no:string,

from_date:date, to_date:date)

employees(emp_no:int, birth_date:date, first_name:string,

last_name:string, gender:enum, hire_date:date)

salaries(emp_no:int, salary:int, from_date:date, to_date:date)

title(emp_no:int, title:string, from_date:date, to_date:date)

2

Exercise 1 – Employee Dataset

22.03.2018

Page 3: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

Average salaries per department

3

Exercise 1 – Employee Dataset

Wrong projection

Missing filters

22.03.2018

Page 4: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

Salaries

4

Exercise 1 – Employee Dataset

Average of current

salaries per department

Department managers

Managers with higher salary than average

22.03.2018

Page 5: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

Employees names

5

Exercise 1 – Employee Dataset

Wrong index

It only matches the first letter

22.03.2018

Page 6: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

First employees

6

Exercise 1 – Employee Dataset

SELECT

e.first_name, e.last_name

FROM employees e

ORDER BY e.hire_date, e.last_name ASC

LIMIT 10

SELECT * FROM (

) AS res

ORDER BY last_name ASC

22.03.2018

Page 7: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

1998

7

Exercise 1 – Employee Dataset

FROM salaries s, dept_emp de, departments dp

SELECT

dp.dept_no AS deptnumber,

dp.dept_name AS deptname,

avg(salary) AS avg_salary

WHERE

de.emp_no = s.emp_no

AND dp.dept_no = de.dept_no

AND date_part(’year’, s.from_date) = 1988

GROUP BY dp.dept_no

22.03.2018

Page 8: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

Current managers

8

Exercise 1 – Employee Dataset

FROM employees e

SELECT

e.first_name, e.last_name, t.title,

d.dept_name, s.salary

JOIN dept_manager dm ON dm.emp_no = e.emp_no

JOIN departments d ON dm.dept_no = d.dept_no

JOIN titles t ON t.emp_no = e.emp_no

JOIN salaries s ON s.emp_no = e.emp_no

WHERE

dm.to_date > NOW()

AND t.to_date > NOW()

AND s.to_date > NOW()

22.03.2018

Page 9: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group 9

Exercise 2 – ZVV Dataset

22.03.2018

Page 10: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

TPCH database schema

10

Exercise 3 – TPCH Dataset

22.03.2018

Page 11: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

Nations from multiple regions

11

Exercise 3 – TPCH Dataset

SELECT nationname

FROM

nation, region

WHERE

nation.regionid = region.regionid AND

( regionname = ’AMERICA’ OR

regionname = ’AFRICA’ OR

regionname = ’ASIA’ )

22.03.2018

Page 12: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

Exclusive supplier

12

Exercise 3 – TPCH Dataset

SELECT COUNT(partid)

FROM part

WHERE partid IN

(SELECT partid

FROM supplypart sp, supplier su

WHERE sp.supplierid = su.supplierid AND

su.suppliername = ’Supplier#000000001’)

(SELECT partid

FROM supplypart sp, supplier su

WHERE sp.supplierid = su.supplierid AND

su.suppliername = ’Supplier#000000002’)

AND partid NOT IN

22.03.2018

Page 13: Exercise 04 SQL I -  · Exercise 04 –SQL I. Systems ... TPCH database schema 10 Exercise 3 –TPCH Dataset 22.03.2018. Systems Group | | Nations from multiple regions 11 Exercise

||Systems Group

First order

13

Exercise 3 – TPCH Dataset

SELECT

orderdate

FROM

orders o, customer c

WHERE

o.customerid = c.customerid AND

c.customername = ’Customer#000000001’

ORDER BY orderdate ASC

LIMIT 1;

22.03.2018