2 min read•august 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
clause to specify the columns to be retrieved from the databaseSELECT
keyword with a comma-separated list of column names or expressionsFROM
clause to indicate the table or tables from which the data will be retrievedFROM
keyword, specify the name of the table or tablesAS
keyword to provide more readable or meaningful names
SELECT first_name AS "First Name"
)FROM employees e
)*
in the SELECT
clause (SELECT * FROM employees
)SELECT
keywordSELECT first_name, last_name, email FROM employees
)SELECT
clause (e.g., SELECT salary * 1.1 AS "New Salary" FROM employees
)DISTINCT
keyword to remove duplicate rows from the 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+
, -
, *
, and /
SELECT salary + bonus AS "Total Compensation" FROM employees
SELECT price - discount AS "Discounted Price" FROM products
SELECT quantity * unit_price AS "Total Amount" FROM order_items
SELECT total_sales / num_employees AS "Sales per Employee" FROM departments
||
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
SELECT 'Employee: ' || first_name || ' ' || last_name AS "Employee Name" FROM employees
)