Home Up PDF Prof. Dr. Ingo Claßen
Classroom Exercises - DSML

Single Table

1

  • Title of all projects whose budget is greater than 20000
  • Order by title
┌────────────────────────┐
│         Title          │
│        varchar         │
├────────────────────────┤
│ Plant Restructurierung │
│ Strategy               │
└────────────────────────┘

2

  • Title of all projects that don't have a budget
  • Order by title
┌─────────┐
│  Title  │
│ varchar │
├─────────┤
│ Sales   │
└─────────┘

3

  • EID and last name of all employees of org unit 17 that have a bonus
  • Order by EID
┌───────┬──────────┐
│  EID  │ LastName │
│ int32 │ varchar  │
├───────┼──────────┤
│   117 │ Oezdem   │
│   118 │ Okeke    │
└───────┴──────────┘

4

  • All columns of all employees whose name starts with letter S
  • Order by EID
┌───────┬───────┬──────────┬────────────┬──────────────┬──────────────┐
│  EID  │ OUID  │ LastName │  Hiredate  │    Salary    │    Bonus     │
│ int32 │ int32 │ varchar  │    date    │ decimal(9,2) │ decimal(9,2) │
├───────┼───────┼──────────┼────────────┼──────────────┼──────────────┤
│   104 │    13 │ Stone    │ 2006-06-01 │     42000.00 │         NULL │
│   108 │    14 │ Sanchez  │ 2014-04-01 │     39000.00 │      1500.00 │
│   112 │    16 │ Singh    │ 2012-03-01 │     43000.00 │      2100.00 │
└───────┴───────┴──────────┴────────────┴──────────────┴──────────────┘

5

  • Name and duration of all titles that are longer than 400 sec
  • Order by duration descending
┌──────────────────┬───────────────┐
│       Name       │ DurationInSec │
│     varchar      │     int32     │
├──────────────────┼───────────────┤
│ Are We Here?     │           934 │
│ Sad But True     │           469 │
│ Butterfly Caught │           455 │
│ The World Is Our │           432 │
└──────────────────┴───────────────┘

6

  • Names of all playlists whose last call was between Sep. 16 and Sep. 17, 2025
  • Order by name
┌──────────────┬────────────┐
│     Name     │  LastCall  │
│   varchar    │    date    │
├──────────────┼────────────┤
│ chillout     │ 2025-09-16 │
│ danceout     │ 2025-09-17 │
│ hard n heavy │ 2025-09-16 │
└──────────────┴────────────┘

7

  • TID, name and duration band of all titles
  • Bands:
    • durationinsec < 300: short
    • 300 <= durationinsec < 400: medium
    • 400 <= durationinsec < 800: long
    • durationinsec >= 800: very long
  • Order by durationinsec
┌───────┬──────────────────┬───────────┐
│  TID  │       Name       │   band    │
│ int32 │     varchar      │  varchar  │
├───────┼──────────────────┼───────────┤
│     2 │ I Will Fade      │ short     │
│     3 │ Out of Sight     │ short     │
│    10 │ Soul for Me      │ short     │
│     6 │ Aotearoa         │ short     │
│     8 │ Science Friction │ medium    │
│    11 │ Idioteque        │ medium    │
│    13 │ Remedies         │ medium    │
│     4 │ Gorecki          │ medium    │
│     1 │ Sit Back Down    │ medium    │
│    12 │ The World Is Our │ long      │
│     5 │ Butterfly Caught │ long      │
│     7 │ Sad But True     │ long      │
│     9 │ Are We Here?     │ very long │
├───────┴──────────────────┴───────────┤
│ 13 rows                    3 columns │
└──────────────────────────────────────┘

8

  • Name durationinsec, and duration in minutes:seconds of all titles
  • Order by name
┌──────────────────┬───────────────┬──────────┐
│       Name       │ DurationInSec │ duration │
│     varchar      │     int32     │ varchar  │
├──────────────────┼───────────────┼──────────┤
│ Aotearoa         │           266 │ 04:26    │
│ Are We Here?     │           934 │ 15:34    │
│ Butterfly Caught │           455 │ 07:35    │
│ Gorecki          │           390 │ 06:30    │
│ I Will Fade      │           189 │ 03:09    │
│ Idioteque        │           309 │ 05:09    │
│ Out of Sight     │           236 │ 03:56    │
│ Remedies         │           378 │ 06:18    │
│ Sad But True     │           469 │ 07:49    │
│ Science Friction │           304 │ 05:04    │
│ Sit Back Down    │           396 │ 06:36    │
│ Soul for Me      │           243 │ 04:03    │
│ The World Is Our │           432 │ 07:12    │
├──────────────────┴───────────────┴──────────┤
│ 13 rows                           3 columns │
└─────────────────────────────────────────────┘

Join

1

  • All playlists together with all titles on them
  • Order by playlist, title
┌──────────────┬──────────────────┐
│   playlist   │      title       │
│   varchar    │     varchar      │
├──────────────┼──────────────────┤
│ chillout     │ Butterfly Caught │
│ chillout     │ I Will Fade      │
│ chillout     │ Sit Back Down    │
│ chillout     │ Soul for Me      │
│ danceout     │ Are We Here?     │
│ danceout     │ Sad But True     │
│ danceout     │ Science Friction │
│ epic         │ Aotearoa         │
│ epic         │ Gorecki          │
│ epic         │ Idioteque        │
│ epic         │ Out of Sight     │
│ hard n heavy │ Remedies         │
│ hard n heavy │ The World Is Our │
├──────────────┴──────────────────┤
│ 13 rows               2 columns │
└─────────────────────────────────┘

2

  • All genre together with all titles belonging to them
  • Order by genre, title
┌──────────┬──────────────────┐
│  genre   │      title       │
│ varchar  │     varchar      │
├──────────┼──────────────────┤
│ Pop      │ I Will Fade      │
│ Pop      │ Sit Back Down    │
│ Rock     │ Idioteque        │
│ Rock     │ Remedies         │
│ Rock     │ The World Is Our │
│ Trip Hop │ Aotearoa         │
│ Trip Hop │ Are We Here?     │
│ Trip Hop │ Butterfly Caught │
│ Trip Hop │ Gorecki          │
│ Trip Hop │ Out of Sight     │
│ Trip Hop │ Sad But True     │
│ Trip Hop │ Science Friction │
│ Trip Hop │ Soul for Me      │
├──────────┴──────────────────┤
│ 13 rows           2 columns │
└─────────────────────────────┘

3

  • For each playlist: which genres are on it
  • Use distinct
  • Order by playlist
┌──────────────┬──────────┐
│   playlist   │  genre   │
│   varchar    │ varchar  │
├──────────────┼──────────┤
│ chillout     │ Pop      │
│ chillout     │ Trip Hop │
│ danceout     │ Trip Hop │
│ epic         │ Trip Hop │
│ epic         │ Rock     │
│ hard n heavy │ Rock     │
└──────────────┴──────────┘

4

  • Name of all Bosses
  • Order by bossname
┌──────────┐
│ bossname │
│ varchar  │
├──────────┤
│ Blaschke │
│ Durmaz   │
│ Li       │
│ Ortega   │
│ Patil    │
│ Umarani  │
│ Umarani  │
└──────────┘

5

  • Name of all orgunits that are related to projects, i.e. where at least one employee of that orgunit is involved in a project
  • Use distinct
  • Order by project, orgunit
┌────────────────────────┬────────────────┐
│        project         │    orgunit     │
│        varchar         │    varchar     │
├────────────────────────┼────────────────┤
│ CRM                    │ Administration │
│ CRM                    │ HR             │
│ Plant Restructurierung │ Production     │
│ Plant Restructurierung │ Warehouse      │
│ Strategy               │ Administration │
│ Strategy               │ Company        │
│ Strategy               │ Production     │
└────────────────────────┴────────────────┘

6

  • Who earns more than his boss
  • Order by boss
┌──────────┬─────────┬──────────────┬──────────────┐
│ employee │  boss   │     esal     │     bsal     │
│ varchar  │ varchar │ decimal(9,2) │ decimal(9,2) │
├──────────┼─────────┼──────────────┼──────────────┤
│ Jadhav   │ Ortega  │     91000.00 │     90000.00 │
└──────────┴─────────┴──────────────┴──────────────┘

7

  • All genres that are not related to any title
  • Order by genre
┌─────────┐
│  genre  │
│ varchar │
├─────────┤
│ Folk    │
└─────────┘

8

  • Project costs per employee
    • 40 hours per week
    • Project lasts exactly one year
    • Only labor costs, no material costs
    • Only salary is considered
  • Order by
┌────────────────────────┬──────────┬───────┐
│        project         │ employee │ cost  │
│        varchar         │ varchar  │ int32 │
├────────────────────────┼──────────┼───────┤
│ CRM                    │ Blaschke │ 18600 │
│ CRM                    │ Durmaz   │ 12000 │
│ Plant Restructurierung │ Oezdem   │  3300 │
│ Plant Restructurierung │ Okeke    │  6400 │
│ Plant Restructurierung │ Popov    │  3400 │
│ Plant Restructurierung │ Umarani  │  7100 │
│ Strategy               │ Durmaz   │ 12000 │
│ Strategy               │ Patil    │ 18000 │
│ Strategy               │ Umarani  │ 14200 │
└────────────────────────┴──────────┴───────┘

Aggregation

1

  • Total duration and number of records per genre
  • Order by genre
┌──────────┬───────────────┬─────────────┐
│  genre   │ totalduration │ noofrecords │
│ varchar  │    int128     │    int64    │
├──────────┼───────────────┼─────────────┤
│ Pop      │           585 │           2 │
│ Rock     │          1119 │           3 │
│ Trip Hop │          3297 │           8 │
└──────────┴───────────────┴─────────────┘

2

  • Number of employees per project
  • Only projects with more than 2 members should be in the output
  • Order by project
┌────────────────────────┬──────────┐
│        project         │ NoOfEmps │
│        varchar         │  int64   │
├────────────────────────┼──────────┤
│ Plant Restructurierung │        4 │
│ Strategy               │        3 │
└────────────────────────┴──────────┘

3

  • Name of orgunit and number of employees
  • Only employees whose salary exceeds 59000
  • Order by orgunit
┌────────────────┬──────────┐
│    orgunit     │ NoOfEmps │
│    varchar     │  int64   │
├────────────────┼──────────┤
│ Accounting     │        1 │
│ Administration │        1 │
│ Company        │        1 │
│ HR             │        1 │
│ Plant          │        2 │
│ Production     │        1 │
└────────────────┴──────────┘

4

  • Cost per project
    • 40 hours per week
    • Project lasts exactly one year
    • Only labor costs, no material costs
    • Only salary is considered
  • Order by project
┌────────────────────────┬───────┐
│        project         │ cost  │
│        varchar         │ int32 │
├────────────────────────┼───────┤
│ CRM                    │ 30600 │
│ Plant Restructurierung │ 20200 │
│ Sales                  │     0 │
│ Strategy               │ 44200 │
└────────────────────────┴───────┘

5

  • Projects whose cost overrun budget
    • 40 hours per week
    • Project lasts exactly one year
    • Only labor costs, no material costs
    • Only salary is considered
  • xxx
  • Order by project
┌─────────┬───────────────┬───────┐
│ project │    Budget     │ cost  │
│ varchar │ decimal(13,2) │ int32 │
├─────────┼───────────────┼───────┤
│ CRM     │      20000.00 │ 30600 │
└─────────┴───────────────┴───────┘

6

  • Number of Customers wrt geography hierarchy
  • Order by number_of_customers
┌─────────┬──────────┬───────────────┬─────────────────────┐
│ Country │  State   │     City      │ number_of_customers │
│ varchar │ varchar  │    varchar    │        int64        │
├─────────┼──────────┼───────────────┼─────────────────────┤
│ Canada  │ BC       │ Shawnee       │                 111 │
│ USA     │ CA       │ Downey        │                 110 │
│ USA     │ OR       │ Lebanon       │                 108 │
│ USA     │ CA       │ Lemon Grove   │                 107 │
│ USA     │ CA       │ Concord       │                 106 │
│ Mexico  │ Guerrero │ Acapulco      │                 106 │
│ USA     │ CA       │ Pomona        │                 106 │
│ USA     │ WA       │ Burien        │                 106 │
│ Canada  │ BC       │ Langford      │                 105 │
│ USA     │ OR       │ Lake Oswego   │                 104 │
│  ·      │ ·        │      ·        │                   · │
│  ·      │ ·        │      ·        │                   · │
│  ·      │ ·        │      ·        │                   · │
│ USA     │ CA       │ Mill Valley   │                  85 │
│ USA     │ WA       │ Spokane       │                  85 │
│ USA     │ OR       │ Albany        │                  84 │
│ USA     │ CA       │ Colma         │                  84 │
│ USA     │ CA       │ Redwood City  │                  84 │
│ USA     │ CA       │ Lakewood      │                  82 │
│ Mexico  │ Sinaloa  │ La Cruz       │                  78 │
│ USA     │ CA       │ Fremont       │                  75 │
│ Mexico  │ DF       │ Santa Fe      │                  51 │
│ USA     │ CA       │ San Francisco │                  47 │
├─────────┴──────────┴───────────────┴─────────────────────┤
│ 109 rows (20 shown)                            4 columns │
└──────────────────────────────────────────────────────────┘

7

  • Revenue, Unitsales, Order Count per year, country and product family
  • Order by tyear, store.country, product.family
┌───────┬─────────┬────────────────┬───────────────┬────────────────┬──────────────┐
│ TYear │ Country │     Family     │    revenue    │ sum(unitsales) │ count_star() │
│ int32 │ varchar │    varchar     │ decimal(38,2) │     int128     │    int64     │
├───────┼─────────┼────────────────┼───────────────┼────────────────┼──────────────┤
│  2014 │ USA     │ Drink          │      48836.21 │          24597 │         7978 │
│  2014 │ USA     │ Food           │     409035.59 │         191940 │        62445 │
│  2014 │ USA     │ Non-Consumable │     107366.33 │          50236 │        16414 │
│  2015 │ Canada  │ Drink          │       8669.84 │           4354 │         1380 │
│  2015 │ Canada  │ Food           │      70028.98 │          32691 │        10375 │
│  2015 │ Canada  │ Non-Consumable │      19346.64 │           9112 │         2877 │
│  2015 │ Mexico  │ Drink          │      36890.31 │          18483 │         5921 │
│  2015 │ Mexico  │ Food           │     311041.43 │         146642 │        47052 │
│  2015 │ Mexico  │ Non-Consumable │      82361.85 │          38789 │        12483 │
│  2015 │ USA     │ Drink          │      48182.01 │          24117 │         7811 │
│  2015 │ USA     │ Food           │     397065.39 │         186590 │        60716 │
│  2015 │ USA     │ Non-Consumable │     105561.02 │          49209 │        15943 │
├───────┴─────────┴────────────────┴───────────────┴────────────────┴──────────────┤
│ 12 rows                                                                6 columns │
└──────────────────────────────────────────────────────────────────────────────────┘

Subquery

x

  • xxx
  • xxx
  • xxx
  • Order by
xxx

Window Functions

x

  • xxx
  • xxx
  • xxx
  • Order by
xxx