A SQL guide to using SELECT, FROM and WHERE

May 8, 2023 | SQL

sql-introduction-kwamehq

To support my work, this post may contain affiliate links (these are referrer links sharing products and services. I get paid a small commission if you make a purchase through the link)

A SQL guide to using SELECT, FROM and WHERE

SQL is a powerful tool for managing and manipulating data in a database.

In this blog post, we’ll explore the first three SQL commands and provide examples of how to use them effectively.

SQL Introduction: A guide to using SELECT, FROM and WHERE

1. USING SELECT

The SELECT command is one of the most basic and essential SQL commands. It is used to retrieve data from a database table.

Here are three examples of how to use the SELECT command:

Example 1: Retrieve all columns from a table


SELECT *
FROM customers;

This query retrieves all the columns from the customers table.

customer_idfirst_namelast_nameemailphonecity
1JohnDoejohndoe@xyz.com555-123-4567New York
2JaneSmithjanesmith@xyz.com555-234-5678Los Angeles
3BobJohnsonbobjohnson@xyz.com555-345-6789Chicago
4MaryBrownmarybrown@xyz.com555-456-7890Houston

Example 2: Retrieve specific columns from a table


SELECT first_name, last_name, email
FROM customers;

This query retrieves only the first name, last name, and email columns from the customers table.

first_namelast_nameemail
JohnDoejohndoe@xyz.com
JaneSmithjanesmith@xyz.com
BobJohnsonbobjohnson@xyz.com
MaryBrownmarybrown@xyz.com

Example 3: Retrieve data based on a condition


SELECT first_name, last_name, email
FROM customers;
WHERE first_name = 'John'

This query retrieves only the first name, last name, and email columns from the customers table where first name is John

first_namelast_nameemail
JohnDoejohndoe@xyz.com

2. USING FROM

The FROM command is used to specify the table or tables from which data is to be retrieved.

Here are three examples of how to use the FROM command:

Example 1: Retrieve data from a single table


SELECT *
FROM customers;

This query retrieves all the columns from the customers table.

customer_idfirst_namelast_nameemailphonecity
1JohnDoejohndoe@xyz.com555-123-4567New York
2JaneSmithjanesmith@xyz.com555-234-5678Los Angeles
3BobJohnsonbobjohnson@xyz.com555-345-6789Chicago
4MaryBrownmarybrown@xyz.com555-456-7890Houston

Example 2: Retrieve data from multiple tables using JOIN


SELECT customers.first_name, orders.order_date
FROM customers
JOIN orders 
ON customers.customer_id = orders.customer_id;

This query retrieves the first name of each customer and the date of each order by joining the customers and orders tables on the customer_id column. Only customers who have made an order will be returned in the output.

first_nameorder_date
John2022-01-02
Jane2022-02-05
Bob2022-02-22

Example 3: Retrieve data from a subquery


SELECT *
FROM (
    SELECT first_name, last_name, city
    FROM customers
    WHERE city = 'New York'
) AS ny_customers;

This query retrieves the first name, last name and city of customers who live in New York by using a subquery to filter the data and then using the subquery as a table in the outer query.

first_namelast_namecity
JohnDoeNew York

3. USING WHERE

The WHERE command is used to filter rows based on a specified condition.

Here are three examples of how to use the WHERE command:

Example 1: Filter rows based on a single condition


SELECT *
FROM customers
WHERE city = 'Los Angeles';

This query retrieves all the columns from the customers table where the city is Los Angeles.

customer_idfirst_namelast_nameemailphonecity
2JaneSmithjanesmith@xyz.com555-234-5678Los Angeles

Example 2: Filter rows based on multiple conditions


SELECT *
FROM orders
WHERE order_date >= '2022-01-01' 
AND total_amount > 1000;

This query retrieves all the columns from the orders table where the order date is on or after January 1, 2022, and the total amount is greater than 1000.

order_idcustomer_idorder_datetotal_amount
222022-02-05399.99

Example 3: Filter rows based on a subquery


SELECT *
FROM orders
WHERE customer_id IN (
    SELECT customer_id
    FROM customers
    WHERE city = 'New York'
);

This query retrieves all the columns from the orders table where the customer_id is in the list of customer_ids returned by the subquery, which filters the customers table to only those who live in New York.

order_idcustomer_idorder_datetotal_amount
112022-01-02120.50

Conclusion

In conclusion, the SELECT, FROM, and WHERE commands are the foundation of SQL programming.

You can write simple to complex SQL queries and manipulate data effectively by mastering these commands.

0 Comments

Submit a Comment

Pin It on Pinterest