Beginner MySQL

How to Write a SELECT Statement in MySQL

Learn the fundamentals of retrieving data from MySQL tables using SELECT, choosing columns, and aliasing results.

5 min read Tutorial

The SELECT statement is the most frequently used command in SQL. It retrieves rows and columns from one or more tables and is the foundation of every MySQL query you will ever write. Understanding SELECT thoroughly will make every other SQL concept easier to learn.

Selecting All Columns

The simplest form of a SELECT query uses the asterisk wildcard to return every column in a table. While convenient for exploration, you should avoid SELECT * in production code because it fetches more data than necessary and breaks if the table schema changes.

SELECT *
FROM employees;

Choosing Specific Columns

Listing only the columns you need makes your query faster and your results easier to read. Separate column names with commas after the SELECT keyword.

SELECT first_name, last_name, email
FROM employees;

Using Column Aliases

Aliases rename columns in the output. Use the AS keyword to give a column a more readable or descriptive name. This is especially useful when working with expressions or when column names in the database are not user-friendly.

SELECT
  first_name AS "First Name",
  last_name  AS "Last Name",
  salary * 12 AS annual_salary
FROM employees;

Removing Duplicates with DISTINCT

When a column contains repeated values, DISTINCT filters out duplicates so each value appears only once in the result set. This is helpful for discovering the unique entries in a column.

SELECT DISTINCT department
FROM employees;

Adding Computed Columns

You can include expressions in your SELECT list. MySQL evaluates them for every row and returns the computed value as a virtual column. Combine expressions with aliases for clarity.

SELECT
  product_name,
  price,
  quantity,
  price * quantity AS total_value
FROM products;

Best Practices

Always list explicit columns instead of using SELECT * in application code. Use meaningful aliases when your column names are ambiguous. Keep your SELECT list organized -- one column per line for readability in complex queries. These small habits will save you debugging time as your queries grow in complexity.

Try this query in UnifySQL

Write, optimize, and collaborate on MySQL queries with AI assistance.

Start Free