DEV Community

Koryn
Koryn

Posted on • Updated on

The Beginner SQL Select Kit

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

Retrieves all data from the table

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

Retrieves John from the table

Query

SELECT * FROM customers WHERE name='John'
SELECT * FROM customers WHERE name='john'

Result

ID name age email
3 John 33 John@testExample.com

Retrieves all Customer names from the table

Query

SELECT name FROM customers

Result

name
Jade
Janet
John
Josh
Jessica
Jack

Retrieves the name and ages of the customers that are between 21 and 50 years old

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

Retrieves the name and email of customers that are 21, 50, or 75 years old

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

Retrieves the name and email of customers that have names that start with 'Ja'

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

Top comments (0)