The Beginner SQL Select Kit
Tags: sql
Published: 2020-09-18 18:19:11

You're given the following SQL table and are asked to retrieve specific Customer data.  

Table name: customers

| ID      | name         | age  | email  |
| ------------- |:-------------:| -----:| -----:|
| 1      | Jade | 21 |    `Jade@testExample.com` |
| 2     | Janet      |   50 |    `Janet.ln@testExample.com` |
| 3 | John      |    33 |    `John@testExample.com` |
| 4 | Josh      |    18 |    `Josh@testExample.com` |
| 5 | Jessica      |    41 |    `Jssica@testExample.com` |
| 6 | Jack      |    75 |    `Jack1@testExample.com` |

SQL servers are by default case insensitive. The following queries will assume assume that.

Keywords
* SELECT: Get this data
* WHERE: Data fitting this description
* BETWEEN: In the following range
* IN: Used with WHERE keyword to find data that fits a list of descriptions
* LIKE: Used with the WHERE keyword to find data that contains the following substring

Functions
* ROUND: Returns that value passed rounded to X decimal places, or when given a negative rounded to the nearest 10th
* LENGTH: Returns amount of characters in a STRING

<center>
### Retrieves all data from the table
</center>

#### Query
```
SELECT * FROM customers
```
**Note:** The * between SELECT and FROM represents all fields.
#### Result

| ID      | name         | age  | email  |
| ------------- |:-------------:| -----:| -----:|
| 1      | Jade | 21 |    `Jade@testExample.com` |
| 2     | Janet      |   50 |    `Janet.ln@testExample.com` |
| 3 | John      |    33 |    `John@testExample.com` |
| 4 | Josh      |    18 |    `Josh@testExample.com` |
| 5 | Jessica      |    41 |    `Jssica@testExample.com` |
| 6 | Jack      |    75 |    `Jack1@testExample.com` |

<center>
### Retrieves John from the table
</center>

#### Query
```
SELECT * FROM customers WHERE name='John'
SELECT * FROM customers WHERE name='john'
```
#### Result

| ID      | name         | age  | email  |
| ------------- |:-------------:| -----:| -----:|
| 3 | John      |    33 |    `John@testExample.com` |

<center>
### Retrieves all Customer names from the table
</center>

#### Query
```
SELECT name FROM customers
```
#### Result
| name         |
| ------------- |
| Jade |
| Janet |
| John |
| Josh |
| Jessica |
| Jack |

<center>
### Retrieves the name and ages of the customers that are between 21 and 50 years old 
</center>

#### Query
```
SELECT name, age FROM customers WHERE age BETWEEN 21 AND 50
```
#### Result
The query is inclusive, so the customers with the ages 21 and 50 are also returned.

| name         | age  |
| ------------- |:-------------:|
| Jade | 21 |
| Janet      |   50 |
| John      |    33 |
| Jessica      |    41 |

<center>
### Retrieves the name and email of customers that are 21, 50, or 75 years old
</center>

#### Query
```
SELECT name, email FROM customers WHERE age IN (21, 50, 75);
```
#### Result
| name         | email  |
| ------------- |:-------------:| 
| Jade | `Jade@testExample.com` |
| Janet      |`Janet.ln@testExample.com` |
| Jack      |`Jack1@testExample.com` |

<center>
### Retrieves the name and email of customers that have names that start with 'Ja'
</center>

#### Query
```
SELECT name, email FROM customers WHERE name LIKE 'Ja%';
```
#### Result
| name         | email  |
| ------------- |:-------------:| 
| Jade | `Jade@testExample.com` |
| Janet      |`Janet.ln@testExample.com` |
| Jack      |`Jack1@testExample.com` |