|
|
|
|
|
|
|
|
# How to retrieve data from a single table |
|
|
# How to retrieve data from a single table |
|
|
|
|
|
|
|
|
## ![Question](question.png) Exercise 1 |
|
|
|
|
|
|
|
|
## ![Question](question-mark.png) Exercise 1 |
|
|
|
|
|
|
|
|
1. Write a SELECT statement that returns three columns from the Vendors table: |
|
|
1. Write a SELECT statement that returns three columns from the Vendors table: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3. Add an ORDER BY clause to this statement that sorts the result set by last name and then first name, both in ascending sequence. Then, run this state‐ment again to make sure it works correctly. |
|
|
3. Add an ORDER BY clause to this statement that sorts the result set by last name and then first name, both in ascending sequence. Then, run this state‐ment again to make sure it works correctly. |
|
|
|
|
|
|
|
|
## ![Question](bulb.png) Solution 1 |
|
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 1 |
|
|
|
|
|
|
|
|
```sql |
|
|
```sql |
|
|
SELECT |
|
|
SELECT |
|
|
|
|
|
|
|
|
vendor_contact_first_name ASC; |
|
|
vendor_contact_first_name ASC; |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## ![Question](question.png) Exercise 2 |
|
|
|
|
|
|
|
|
## ![Question](question-mark.png) Exercise 2 |
|
|
|
|
|
|
|
|
1. Write a SELECT statement that returns one column from the Vendors table named full_name that joins the **vendor_contact_last_name** and **vendor_contact_first_name** columns. Format this column with the last name, a comma, a space, and the first name like this: Doe, John |
|
|
1. Write a SELECT statement that returns one column from the Vendors table named full_name that joins the **vendor_contact_last_name** and **vendor_contact_first_name** columns. Format this column with the last name, a comma, a space, and the first name like this: Doe, John |
|
|
|
|
|
|
|
|
2. Sort the result set by last name and then first name in ascending sequence. Return only the contacts whose last name begins with the letter A, B, C, or E. This should retrieve 41 rows. |
|
|
2. Sort the result set by last name and then first name in ascending sequence. Return only the contacts whose last name begins with the letter A, B, C, or E. This should retrieve 41 rows. |
|
|
|
|
|
|
|
|
## ![Question](bulb.png) Solution 2 |
|
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 2 |
|
|
|
|
|
|
|
|
```sql |
|
|
```sql |
|
|
SELECT CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS 'full_name' |
|
|
SELECT CONCAT(vendor_contact_last_name, ', ', vendor_contact_first_name) AS 'full_name' |
|
|
|
|
|
|
|
|
ORDER BY vendor_contact_last_name ASC; |
|
|
ORDER BY vendor_contact_last_name ASC; |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## ![Question](question.png) Exercise 3 |
|
|
|
|
|
|
|
|
## ![Question](question-mark.png) Exercise 3 |
|
|
|
|
|
|
|
|
1. Write a SELECT statement that returns these column names and data from the Invoices table: |
|
|
1. Write a SELECT statement that returns these column names and data from the Invoices table: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3. Sort the result set in descending sequence by invoice_due_date. |
|
|
3. Sort the result set in descending sequence by invoice_due_date. |
|
|
|
|
|
|
|
|
## ![Question](bulb.png) Solution 3 |
|
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 3 |
|
|
|
|
|
|
|
|
```sql |
|
|
```sql |
|
|
SELECT |
|
|
SELECT |
|
|
|
|
|
|
|
|
ORDER BY invoice_due_date DESC; |
|
|
ORDER BY invoice_due_date DESC; |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## ![Question](question.png) Exercise 4 |
|
|
|
|
|
|
|
|
## ![Question](question-mark.png) Exercise 4 |
|
|
|
|
|
|
|
|
1. Write a SELECT statement that returns these columns from the Invoices table: |
|
|
1. Write a SELECT statement that returns these columns from the Invoices table: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4. Use the LIMIT clause so the result set contains only the rows with the 5 largest balances. |
|
|
4. Use the LIMIT clause so the result set contains only the rows with the 5 largest balances. |
|
|
|
|
|
|
|
|
## ![Question](bulb.png) Solution 4 |
|
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 4 |
|
|
|
|
|
|
|
|
```sql |
|
|
```sql |
|
|
SELECT |
|
|
SELECT |
|
|
|
|
|
|
|
|
LIMIT 5; |
|
|
LIMIT 5; |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## ![Question](question.png) Exercise 5 |
|
|
|
|
|
|
|
|
## ![Question](question-mark.png) Exercise 5 |
|
|
|
|
|
|
|
|
1. Write a SELECT statement that returns these columns from the Invoices table: |
|
|
1. Write a SELECT statement that returns these columns from the Invoices table: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2. Return only the rows where the payment_date column contains a null value. This should retrieve 11 rows. |
|
|
2. Return only the rows where the payment_date column contains a null value. This should retrieve 11 rows. |
|
|
|
|
|
|
|
|
## ![Question](bulb.png) Solution 5 |
|
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 5 |
|
|
|
|
|
|
|
|
```sql |
|
|
```sql |
|
|
SELECT |
|
|
SELECT |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## ![Question](question.png) Exercise 6 |
|
|
|
|
|
|
|
|
## ![Question](question-mark.png) Exercise 6 |
|
|
|
|
|
|
|
|
1. Write a SELECT statement without a FROM clause that uses the CURRENT_DATE function to return the current date in its default format. |
|
|
1. Write a SELECT statement without a FROM clause that uses the CURRENT_DATE function to return the current date in its default format. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3. Give this column an alias of current_date. To do that, you must enclose the alias in quotes since that name is already used by the CURRENT_DATE function. |
|
|
3. Give this column an alias of current_date. To do that, you must enclose the alias in quotes since that name is already used by the CURRENT_DATE function. |
|
|
|
|
|
|
|
|
## ![Question](bulb.png) Solution 6 |
|
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 6 |
|
|
|
|
|
|
|
|
```sql |
|
|
```sql |
|
|
SELECT DATE_FORMAT(CURRENT_DATE, '%m-%d-%y') AS 'current_date'; |
|
|
SELECT DATE_FORMAT(CURRENT_DATE, '%m-%d-%y') AS 'current_date'; |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## ![Question](question.png) Exercise 7 |
|
|
|
|
|
|
|
|
## ![Question](question-mark.png) Exercise 7 |
|
|
|
|
|
|
|
|
1. Write a SELECT statement without a FROM clause that creates a row with these columns: |
|
|
1. Write a SELECT statement without a FROM clause that creates a row with these columns: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2. To calculate the third column, add the expressions you used for the first two columns. |
|
|
2. To calculate the third column, add the expressions you used for the first two columns. |
|
|
|
|
|
|
|
|
## ![Question](bulb.png) Solution 7 |
|
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 7 |
|
|
|
|
|
|
|
|
```sql |
|
|
```sql |
|
|
SELECT |
|
|
SELECT |