SQL SELECT Statements

In this lesson, you'll learn how to extract specific information from your marketing data using the `SELECT` and `WHERE` clauses in SQL. You'll master the fundamentals of retrieving data and filtering it based on specific criteria, setting the foundation for more advanced data analysis techniques.

Learning Objectives

  • Understand the purpose and function of the `SELECT` clause.
  • Learn how to select specific columns from a table.
  • Grasp the use of the `WHERE` clause for filtering data based on conditions.
  • Practice combining `SELECT` and `WHERE` clauses to retrieve targeted data.

Text-to-Speech

Listen to the lesson content

Lesson Content

Introduction to the SELECT Clause

The SELECT clause is the most fundamental part of any SQL query. It's how you specify which columns (i.e., data fields) you want to see from a table. Think of it as choosing the specific columns of a spreadsheet you want to view.

Syntax:

SELECT column_name1, column_name2, ...
FROM table_name;
  • column_name1, column_name2, ... : The names of the columns you want to retrieve. Separate multiple column names with commas.
  • FROM table_name: Specifies the table from which you want to retrieve the data.

Example: Let's say you have a table named customers with columns like customer_id, first_name, last_name, and email. To retrieve the first_name and last_name of all customers, you would use:

SELECT first_name, last_name
FROM customers;

To select all columns, you can use the asterisk (*):

SELECT *
FROM customers;

This will return all columns and all rows from the customers table.

Using the WHERE Clause for Filtering

The WHERE clause allows you to filter the data you retrieve, based on a specific condition. This is like applying a filter in a spreadsheet to show only certain rows that meet a particular criterion.

Syntax:

SELECT column_name1, column_name2, ...
FROM table_name
WHERE condition;
  • WHERE condition: Specifies the condition that rows must satisfy to be included in the result. Conditions often involve comparison operators like =, <, >, <=, >=, and != (not equal to).

Examples:

  1. Filtering based on equality: To find customers in the customers table whose city is 'New York':

    sql SELECT first_name, last_name, city FROM customers WHERE city = 'New York';

  2. Filtering based on numeric values: To find orders in an orders table with an order_total greater than 100:

    sql SELECT order_id, order_total FROM orders WHERE order_total > 100;

  3. Filtering using Text and Quotes: SQL needs text entries to be wrapped with quotation marks.

    sql SELECT first_name, last_name, city FROM customers WHERE city = 'London';

Combining SELECT and WHERE

You can use the SELECT and WHERE clauses together to retrieve specific columns and filter rows at the same time. This is where the real power of SQL starts to shine.

Example: To get the email addresses and first_name of customers who are located in 'Paris' from the customers table:

SELECT first_name, email
FROM customers
WHERE city = 'Paris';

This query will first filter the customers table to include only rows where city is 'Paris', and then select the first_name and email columns for those rows.

Common Comparison Operators

The WHERE clause relies on comparison operators to define the filtering criteria. Here's a quick reference:

  • = : Equal to
  • != or <> : Not equal to
  • < : Less than
  • > : Greater than
  • <= : Less than or equal to
  • >= : Greater than or equal to

These operators allow you to compare values in your data and retrieve precisely the information you need.

Progress
0%