Joining Tables

Preparation:

Primary Keyarrow-up-right

Foreign Keyarrow-up-right

Mysql Joinarrow-up-right

MySQL Deletearrow-up-right

Peer instruction

1. What does the following query obtain?

SELECT *
FROM employees
WHERE employee_number = "NULL"

A) Retrieves all employees with an employee number called NULL

B) Retrieves all employees without an employee number

C) Retrieves all employees except employees without an employee number

D) Syntax Error

2. What does the following query obtain?

SELECT *
FROM imdb
WHERE duration > 0
AND duration IS NOT NULL;

A) Retrieves all movies from IMDB with a duration greater than zero and all NULL values

B) Retrieves all movies from IMDB with a duration less than 0

C) Retrieves all movies from IMDB with a duration greater than zero and without NULL values

D) Retrieves all movies from IMDB with a duration greater than zero or without NULL values

Exercise 1: Individuelt (30 min)

  • Join the two tables by department_number and display all

  • Retrieve all the employees from Accounting (using joins)

  • Retrieve the names of all employees from operations and research

Advanced (Optional)

  • Retrieve the names of all salesmen from Chicago

  • Retrieve the id of all clerks from New York

  • Retrieve all data from employees and their department except clerks or employees from New York with a comission

Import dataset to database:

Exercise 2: Par-øvelser

Import the dataset: https://github.com/behu-kea/dat20-classes/blob/master/SQL/assets/coffee-database.sql

image-20220901103151949

Exercises for today & next class

A)

  • Notice that the 'gender' datatype is an ENUM

    • Research: What is an enum? How does it work?

  • Select the order_table id and the customers phone number for all orders of product id 3

  • Select product names and order time for the espresso coffees sold between January 15th 2021 and February 14th 2021

  • Select the product name and price and order time for all orders from females in February 2021

    • THIS WAS CHANGED FROM JANUARY

  • From the products table, select the name for all products that have a price greater than 30 or a coffee origin of Sri Lanka

  • How many male customers don't have a phone number entered into the customers table

Halfway there ! Take a break

B)

  • From the products table, select the name and price of all produts with a coffee origin equal to Colombia or Sverige. Ordered by name from A-Z

  • From the orders table, select all the orders from February 2021 for customers with id's 2, 4, 6 or 8

  • From the customers table, select the first name and phone number of all customers who's last name containing the pattern "ar"

  • From the customers table, select the distinct last names and order alphabetically from A-Z

  • From the orders table, select the first 3 orders placed by customer with id 1 in February 2021

  • From the products table, select the name, price and cofee origin but rename the price to retail_price in the results set

Last updated