|
|
@@ -1,6 +1,10 @@ |
|
|
|
# How to retrieve data from a single table |
|
|
|
|
|
|
|
## ![Question](question-mark.png) Exercise 1 |
|
|
|
<!-- markdownlint-disable MD001 --> |
|
|
|
|
|
|
|
#### ![Question](question-mark.png) Exercise 1 |
|
|
|
|
|
|
|
<!-- markdownlint-enable MD001 --> |
|
|
|
|
|
|
|
1. Write a SELECT statement that returns three columns from the Vendors table: |
|
|
|
|
|
|
@@ -12,7 +16,7 @@ |
|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 1 |
|
|
|
### ![Answer](bulb.png) Solution 1 |
|
|
|
|
|
|
|
```sql |
|
|
|
SELECT |
|
|
@@ -26,13 +30,13 @@ ORDER BY |
|
|
|
vendor_contact_first_name ASC; |
|
|
|
``` |
|
|
|
|
|
|
|
## ![Question](question-mark.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 |
|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 2 |
|
|
|
### ![Answer](bulb.png) Solution 2 |
|
|
|
|
|
|
|
```sql |
|
|
|
SELECT |
|
|
@@ -48,7 +52,7 @@ ORDER BY |
|
|
|
vendor_contact_last_name ASC; |
|
|
|
``` |
|
|
|
|
|
|
|
## ![Question](question-mark.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: |
|
|
|
|
|
|
@@ -61,7 +65,7 @@ ORDER BY |
|
|
|
|
|
|
|
3. Sort the result set in descending sequence by invoice_due_date. |
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 3 |
|
|
|
### ![Answer](bulb.png) Solution 3 |
|
|
|
|
|
|
|
```sql |
|
|
|
SELECT |
|
|
@@ -78,7 +82,7 @@ ORDER BY |
|
|
|
invoice_due_date DESC; |
|
|
|
``` |
|
|
|
|
|
|
|
## ![Question](question-mark.png) Exercise 4 |
|
|
|
### ![Question](question-mark.png) Exercise 4 |
|
|
|
|
|
|
|
1. Write a SELECT statement that returns these columns from the Invoices table: |
|
|
|
|
|
|
@@ -93,7 +97,7 @@ ORDER BY |
|
|
|
|
|
|
|
4. Use the LIMIT clause so the result set contains only the rows with the 5 largest balances. |
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 4 |
|
|
|
### ![Answer](bulb.png) Solution 4 |
|
|
|
|
|
|
|
```sql |
|
|
|
SELECT |
|
|
@@ -110,7 +114,7 @@ ORDER BY |
|
|
|
LIMIT 5; |
|
|
|
``` |
|
|
|
|
|
|
|
## ![Question](question-mark.png) Exercise 5 |
|
|
|
### ![Question](question-mark.png) Exercise 5 |
|
|
|
|
|
|
|
1. Write a SELECT statement that returns these columns from the Invoices table: |
|
|
|
|
|
|
@@ -121,7 +125,7 @@ LIMIT 5; |
|
|
|
|
|
|
|
2. Return only the rows where the payment_date column contains a null value. This should retrieve 11 rows. |
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 5 |
|
|
|
### ![Answer](bulb.png) Solution 5 |
|
|
|
|
|
|
|
```sql |
|
|
|
SELECT |
|
|
@@ -137,7 +141,7 @@ GROUP BY |
|
|
|
invoice_number; |
|
|
|
``` |
|
|
|
|
|
|
|
## ![Question](question-mark.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. |
|
|
|
|
|
|
@@ -145,13 +149,13 @@ GROUP BY |
|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 6 |
|
|
|
### ![Answer](bulb.png) Solution 6 |
|
|
|
|
|
|
|
```sql |
|
|
|
SELECT DATE_FORMAT(CURRENT_DATE, '%m-%d-%y') AS 'current_date'; |
|
|
|
``` |
|
|
|
|
|
|
|
## ![Question](question-mark.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: |
|
|
|
|
|
|
@@ -161,7 +165,7 @@ SELECT DATE_FORMAT(CURRENT_DATE, '%m-%d-%y') AS 'current_date'; |
|
|
|
|
|
|
|
2. To calculate the third column, add the expressions you used for the first two columns. |
|
|
|
|
|
|
|
## ![Answer](bulb.png) Solution 7 |
|
|
|
### ![Answer](bulb.png) Solution 7 |
|
|
|
|
|
|
|
```sql |
|
|
|
SELECT 50000 AS 'starting principal', |