| Story points | 3 |
| Tags | sql skill/databases |
| Hard Prerequisites | |
| IMPORTANT: Please review these prerequisites, they include important information that will help you with this content. | |
|
|
NB! Many learners configure the docker-compose.yaml file to make use of the Adminer image but still instruct reviewers to download and install Postgres on their local machines in the documentation. This indicates a misunderstanding of the relationship between these technologies.
To Clarify:
Ensure the documentation reflects this setup correctly, so reviewers do not need to install Postgres locally. Refer to the following resources for more information:
You should know about clean sql code by now. If you need a refresher please take a look here: TOPIC: Clean Code for SQL
The names of the tables and columns described in this project are funky (not in a good way).
While doing this project, please ensure that all names are clear and adhere to clean code standards.
Save all of your instructions in script files - you will submit these files on Github.
Create a database called “shop”.
Create the following tables in the shop database:
Create a primary key for each table with auto-increment. Ensure you correctly specify the data types (e.g. the ID field should be int).
Create foreign keys so that every ID in the Orders and Payments tables references an existing ID in the tables referenced (e.g., ProductID, EmployeeID, etc).
Insert the records from the tables below into the table created in step 2
Document the information stored in your database. Specify what information is kept in each table and identify the keys that link records between tables. Save this document in a .md format.
| ID (int) | FirstName (varchar50) | LastName (varchar50) | Gender (varchar) | Address (varchar200) | Phone (varchar 20) | Email (varchar100) | City (varchar20) | Country (varchar50) |
|---|---|---|---|---|---|---|---|---|
| 1 | John | Hibert | Male | 284 chaucer st | 084789657 | john@gmail.com | Johannesburg | South Africa |
| 2 | Thando | Sithole | Female | 240 Sect 1 | 0794445584 | thando@gmail.com | Cape Town | South Africa |
| 3 | Leon | Glen | Male | 81 Everton Rd,Gillits | 0820832830 | Leon@gmail.com | Durban | South Africa |
| 4 | Charl | Muller | Male | 290A Dorset Ecke | +44856872553 | Charl.muller@yahoo.com | Berlin | Germany |
| 5 | Julia | Stein | Female | 2 Wernerring | +448672445058 | Js234@yahoo.com | Frankfurt | Germany |
| ID (int) | FirstName (varchar50) | LastName (varchar50) | Email (varchar100) | JobTitle (varchar20) |
|---|---|---|---|---|
| 1 | Kani | Matthew | mat@gmail.com | Manager |
| 2 | Lesly | Cronje | LesC@gmail.com | Clerk |
| 3 | Gideon | Maduku | m@gmail.com | Accountant |
| ID (int) | ProductID (int) | PaymentID (int) | FulfilledByEmployeeID (int) | DateRequired (datetime) | DateShipped (datetime) | Status (varchar20) |
|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 2 | 05-09-2018 | Not shipped | |
| 2 | 1 | 2 | 2 | 04-09-2018 | 03-09-2018 | Shipped |
| 3 | 3 | 3 | 3 | 06-09-2018 | Not shipped |
Note: When creating tables, each table will have an ID column. When joining data between tables, the foreign key must include the name of the table the data came from, e.g., ProductID in the above table.
| ID (int) | CustomerID (int) | PaymentDate (datetime) | Amount (decimal) |
|---|---|---|---|
| 1 | 1 | 01-09-2018 | R150.75 |
| 2 | 5 | 03-09-2018 | R150.75 |
| 3 | 4 | 03-09-2018 | R700.60 |
Note: When creating tables, you will note that each table has an ID column. When joining data between tables, the foreign key must include the name of the table the data came from, i.e. CustomerID in the above table.
#3## Products Table
| ID (int) | ProductName (varchar100) | Description (varchar300) | BuyPrice (decimal) |
|---|---|---|---|
| 1 | Harley Davidson Chopper | This replica features a working kickstand, front suspension, gear-shift lever | R150.75 |
| 2 | Classic Car | Turnable front wheels, steering function | R550.75 |
| 3 | Sportscar | Turnable front wheels, steering function | R700.60 |
Save all of your instructions in script files - you will submit these files on Github.
NB! Be sure to label your answers by placing the question above the corresponding code. Submitting a wall of code without explanations makes it difficult to review.
Select all records from the Customers table.
Select only the FirstName column from the Customers table
Display the full name of the customer with CustomerID 1.
Update the record for CustomerID 1 in the Customers table to change the name to “Lerato Mabitso”.
Delete the record for the customer with CustomerID 2 from the Customers table.
Select all unique statuses from the Orders table and count the number of orders for each status.
Return the maximum payment made in the Payments table.
Select all customers from the Customers table, sorted by the Country column.
Select all products with a price between R100 and R600.
Select all fields from Customers where the Country is “Germany” AND the City is “Berlin”.
Select all fields from Customers where the City is either “Cape Town” OR “Durban”.
Select all records from Products where the price is greater than R500.
Return the total sum of the amounts in the Payments table.
Count the number of shipped orders in the Orders table.
Return the average price of all products, both in Rands and in Dollars (assuming the exchange rate is R12 to the Dollar).
Using an INNER JOIN, create a query that selects all payments along with the corresponding customer information.
Select all products that have turnable front wheels.
Please make sure that the learner used Postgres and not MySQL.
The ID columns of the tables can either be TableName + ID or just ID, either is acceptable due to recent updates.
The following files should be present:
src directory should contain 4 SQL script files: one to create the database, one to create tables, one to populate tables, and one to query the database.Ensure the learner has adapted the naming conventions to match those specified in TOPIC: Clean Code for SQL