Understanding the SELECT Statement in MySQL
Introduction
In this lesson we explore the SELECT statement, the core command for retrieving data from MySQL tables. The tutorial walks through creating a query, selecting columns, limiting rows, using calculations, applying the DISTINCT keyword, and saving your work.
Setting Up the Query
- Open a new editor tab in your MySQL client.
- Write a basic SELECT query:
sql SELECT * FROM employee_demographics; *means all columns.- A trailing semicolon (
;) tells MySQL where the statement ends.
Limiting Rows
- The interface often adds
LIMIT 1000automatically to keep execution fast. - If the table contains more rows (e.g., 50,000 or 1,000,000), adjust or remove the
LIMITclause accordingly.
Specifying the Database
- When multiple databases are open, MySQL uses the one currently highlighted.
- To avoid ambiguity, prefix the table with the database name:
sql SELECT * FROM Parks_and_Recreation.employee_demographics; - This ensures the query runs against the intended database even if another (e.g.,
sys) is selected.
Selecting Specific Columns
- Replace
*with column names separated by commas:sql SELECT first_name, last_name, birth_date FROM employee_demographics; - Columns can be listed on one line or each on its own line for readability.
Using Calculations in SELECT
- You can perform arithmetic directly in the SELECT list:
sql SELECT first_name, age, age + 10 AS age_plus_10 FROM employee_demographics; - MySQL follows PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction) for order of operations.
- Example:
sql SELECT (age + 10) * 10 AS result FROM employee_demographics;The parentheses are evaluated first, then multiplication.
The DISTINCT Keyword
DISTINCTreturns only unique values for the selected column(s).sql SELECT DISTINCT gender FROM employee_demographics;Returnsmaleandfemaleonly.- When multiple columns are listed,
DISTINCTapplies to the combination of those columns, which often results in all rows being unique.
Saving Your Queries
- Click the Save button to store the script in a project folder (e.g.,
2.select_statement_tutorial.sql). - Saved files can be reopened later, edited, or shared via version control such as GitHub.
Recap of Key Commands
SELECT * FROM table;SELECT col1, col2 FROM table;SELECT col, col + 10 AS new_col FROM table;SELECT DISTINCT col FROM table;- Prefix with
database_name.when needed.
Best Practices
- Explicitly specify the database to avoid accidental queries against the wrong schema.
- Use line breaks and indentation for readability, especially when adding functions or calculations.
- Keep
LIMITclauses during exploratory work to reduce load time. - Save scripts frequently to track progress and facilitate collaboration.
Mastering the SELECT statement—choosing columns, limiting rows, performing calculations, and using DISTINCT—gives you the foundation to query MySQL efficiently and safely. By following the demonstrated best practices, you can retrieve exactly the data you need without unnecessary processing.
Frequently Asked Questions
Who is Alex The Analyst on YouTube?
Alex The Analyst is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.
Does this page include the full transcript of the video?
Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.