You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

7.1 SELECT statement fundamentals

2 min readaugust 6, 2024

The statement is the backbone of SQL querying. It lets you grab specific data tables, filter results, and perform calculations. Mastering SELECT opens up a world of data manipulation possibilities.

In this section, we'll cover the basics of SELECT and FROM clauses, aliasing, wildcards, and using . We'll also dive into arithmetic operations and string concatenation to level up your querying skills.

SELECT and FROM Clauses

Specifying Columns and Tables

Top images from around the web for Specifying Columns and Tables
Top images from around the web for Specifying Columns and Tables
  • Use the
    SELECT
    clause to specify the columns to be retrieved from the database
  • Follow the
    SELECT
    keyword with a comma-separated list of column names or expressions
  • Employ the
    FROM
    clause to indicate the table or tables from which the data will be retrieved
  • After the
    FROM
    keyword, specify the name of the table or tables

Aliasing and Wildcards

  • Assign aliases to columns or tables using the
    AS
    keyword to provide more readable or meaningful names
    • Column aliases are defined immediately after the column name or expression (e.g.,
      SELECT first_name AS "First Name"
      )
    • Table aliases are specified after the table name (e.g.,
      FROM employees e
      )
  • Retrieve all columns from a table using the wildcard character
    *
    in the
    SELECT
    clause (
    SELECT * FROM employees
    )

Selecting Specific Columns

  • Choose specific columns to retrieve by listing their names after the
    SELECT
    keyword
  • Separate multiple column names with commas (e.g.,
    SELECT first_name, last_name, email FROM employees
    )
  • Perform calculations or apply functions to columns directly in the
    SELECT
    clause (e.g.,
    SELECT salary * 1.1 AS "New Salary" FROM employees
    )

DISTINCT and Operators

Eliminating Duplicate Rows

  • Use the
    DISTINCT
    keyword to remove duplicate rows from the
  • Place
    DISTINCT
    immediately after the
    SELECT
    keyword (e.g.,
    SELECT DISTINCT department_id FROM employees
    )
  • DISTINCT
    considers the combination of all selected columns when determining uniqueness

Arithmetic Operations

  • Perform arithmetic operations on numeric columns using operators such as
    +
    ,
    -
    ,
    *
    , and
    /
    • Addition:
      SELECT salary + bonus AS "Total Compensation" FROM employees
    • Subtraction:
      SELECT price - discount AS "Discounted Price" FROM products
    • Multiplication:
      SELECT quantity * unit_price AS "Total Amount" FROM order_items
    • Division:
      SELECT total_sales / num_employees AS "Sales per Employee" FROM departments

Concatenating Strings

  • Concatenate string values using the
    ||
    operator or the
    CONCAT()
    function
    • ||
      operator:
      SELECT first_name || ' ' || last_name AS "Full Name" FROM employees
    • CONCAT()
      function:
      SELECT CONCAT(first_name, ' ', last_name) AS "Full Name" FROM employees
  • Combine string literals with column values to create meaningful output (e.g.,
    SELECT 'Employee: ' || first_name || ' ' || last_name AS "Employee Name" FROM employees
    )
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary