Browse Source

published solutions

master
Bart De Lepeleer 2 years ago
parent
commit
1a2064b159
8 changed files with 50 additions and 7 deletions
  1. 7
    7
      README.md
  2. 4
    0
      exercise-1.sql
  3. 6
    0
      exercise-2.sql
  4. 5
    0
      exercise-3.sql
  5. 8
    0
      exercise-4.sql
  6. 7
    0
      exercise-5.sql
  7. 5
    0
      exercise-6.sql
  8. 8
    0
      exercise-7.sql

+ 7
- 7
README.md View File

@@ -1,10 +1,10 @@
# How_to_retrieve_data_from_two_or_more_tables

###Exercise 1
### Exercise 1

Write a SELECT statement that returns all columns from the Vendors table inner-joined with all columns from the Invoices table. This should return 114 rows. Hint: You can use an asterisk (*) to select the columns from both tables.

###Exercise 2
### Exercise 2

Write a SELECT statement that returns these four columns:

@@ -19,7 +19,7 @@ Return one row for each invoice with a non-zero balance. This should return 11 r

Sort the result set by vendor_name in ascending order.

###Exercise 3
### Exercise 3

Write a SELECT statement that returns these three columns:

@@ -31,7 +31,7 @@ Return one row for each vendor. This should return 122 rows.

Sort the result set by account_description and then by vendor_name.

###Exercise 4
### Exercise 4

Write a SELECT statement that returns these five columns:

@@ -45,7 +45,7 @@ Use aliases for the tables. This should return 118 rows.

Sort the final result set by vendor_name, invoice_date, invoice_number, and invoice_sequence.

###Exercise 5
### Exercise 5

Write a SELECT statement that returns three columns:

@@ -59,7 +59,7 @@ Return one row for each vendor whose contact has the same last name as another v

Sort the result set by vendor_contact_last_name.

###Exercise 6
### Exercise 6

Write a SELECT statement that returns these three columns:

@@ -75,7 +75,7 @@ Remove the invoice_id column from the SELECT clause.

Sort the final result set by the account_number column.

###Exercise 7
###E xercise 7

Use the UNION operator to generate a result set consisting of two columns from the Vendors table: vendor_name and vendor_state.


+ 4
- 0
exercise-1.sql View File

@@ -0,0 +1,4 @@
SELECT *
FROM vendors JOIN invoices
ON vendors.vendor_id = invoices.vendor_id

+ 6
- 0
exercise-2.sql View File

@@ -0,0 +1,6 @@
SELECT vendor_name, invoice_number, invoice_date,
invoice_total - payment_total - credit_total AS balance_due
FROM vendors v JOIN invoices i
ON v.vendor_id = i.vendor_id
WHERE invoice_total - payment_total - credit_total <> 0
ORDER BY vendor_name

+ 5
- 0
exercise-3.sql View File

@@ -0,0 +1,5 @@
SELECT vendor_name, default_account_number AS default_account,
account_description AS description
FROM vendors v JOIN general_ledger_accounts gl
ON v.default_account_number = gl.account_number
ORDER BY account_description, vendor_name

+ 8
- 0
exercise-4.sql View File

@@ -0,0 +1,8 @@
SELECT vendor_name, invoice_date, invoice_number,
invoice_sequence AS li_sequence,
line_item_amount AS li_amount
FROM vendors v JOIN invoices i
ON v.vendor_id = i.vendor_id
JOIN invoice_line_items li
ON i.invoice_id = li.invoice_id
ORDER BY vendor_name, invoice_date, invoice_number, invoice_sequence

+ 7
- 0
exercise-5.sql View File

@@ -0,0 +1,7 @@
SELECT v1.vendor_id,
v1.vendor_name,
CONCAT(v1.vendor_contact_first_name, ' ', v1.vendor_contact_last_name) AS contact_name
FROM vendors v1 JOIN vendors v2
ON v1.vendor_id <> v2.vendor_id AND
v1.vendor_contact_last_name = v2.vendor_contact_last_name
ORDER BY v1.vendor_contact_last_name

+ 5
- 0
exercise-6.sql View File

@@ -0,0 +1,5 @@
SELECT gl.account_number, account_description, invoice_id
FROM general_ledger_accounts gl LEFT JOIN invoice_line_items li
ON gl.account_number = li.account_number
WHERE li.invoice_id IS NULL
ORDER BY gl.account_number

+ 8
- 0
exercise-7.sql View File

@@ -0,0 +1,8 @@
SELECT vendor_name, vendor_state
FROM vendors
WHERE vendor_state = 'CA'
UNION
SELECT vendor_name, 'Outside CA'
FROM vendors
WHERE vendor_state <> 'CA'
ORDER BY vendor_name

Loading…
Cancel
Save