MySQL Query Operations: SELECT, Filtering, Sorting, and Pagination

Attitude: Believe that victory is ahead. If you stop because of small issues, you might not be fit to win. The road to success is muddy, but in the end, the mud on you will be medals of honor! Today's motto: Everything that happens benefits me.

Table of Contents

  • SELECT
    1. Selecting All Columns
    2. Selecting Specific Columns
    3. Expressions in Queries
    4. Aliases
    5. Removing Duplicates (DISTINCT)
    6. Sorting (ORDER BY)
    7. Filtering with WHERE
    8. Pagination (LIMIT and OFFSET)

The table created in the previous blog will be used as examples. It's a score table with columns: id, name, chinese, math. Sample table

SELECT

1. Selecting All Columns

SELECT * FROM table_name;

This retrieves every row and column. * is a wildcard. It's generally not recommended because fetching more data increases network load and may affect index usage (to be covered later).

All columns query result

2. Selecting Specific Columns

SELECT column1, column2, ... FROM table_name;

Example: SELECT name FROM student; retrieves all names from the student table.

Specific column query

3. Expressions in Queries

SELECT expression FROM table_name;

Expressions do not modify stored data; they only compute temporary results sent to the client. The original data remains unchanged.

Expression query example

4. Aliases

Use AS to assign a new name to a column or expression.

SELECT column_name, column1 + column2 AS new_name FROM table_name;

AS can be omitted: SELECT name 姓名 FROM student;

Alias example

5. Removing Duplicates (DISTINCT)

SELECT DISTINCT column_name FROM table_name;

Consolidates duplicate rows into one.

DISTINCT example

6. Sorting (ORDER BY)

  • Without ORDER BY, the order is undefined.
  • NULL values are considered smallest in ascending order (first) and largest in descending (last).
  • Can sort by expressions or aliases.
  • Multiple columns: sorted by first column, then second if ties.
SELECT * FROM table_name ORDER BY column1 [ASC|DESC], column2 [ASC|DESC];

ORDER BY example

7. Filtering with WHERE

Operators

Operator Description
>, >=, <, <= Comparison
= Equal (NULL unsafe; NULL = NULL → NULL)
<=> Equal (NULL safe; NULL <=> NULL → TRUE)
!=, <> Not equal
BETWEEN a0 AND a1 Range inclusive
IN (...) Matches any in list
IS NULL Is NULL
IS NOT NULL Is not NULL
LIKE Pattern matching (% for any sequence, _ for single char)

Logical Operators

Operator Description
AND Both conditions true
OR Atleast one true
NOT Negates condition

Notes:

  • Cannot use aliases in WHERE (they are defined after evaluation).
  • AND has higher precedence than OR. Use parentheses to adjust.

Examples: WHERE examples

More WHERE

LIKE usage

8. Pagination (LIMIT and OFFSET)

-- Start from row 0, fetch 3 rows
SELECT * FROM table_name LIMIT 3;

-- Start after row 2, fetch 3 rows (OFFSET 3, LIMIT 2)
SELECT * FROM table_name LIMIT 2 OFFSET 3;

-- Shorthand: LIMIT offset, count
SELECT * FROM table_name LIMIT 1, 3;  -- from row 1, 3 rows

Pagination example

  • Starting index is 0.

If any explanation is incorrect or unclear, please comment. I will update it. Thank you! Hope this helps. Linked list exercises will be updated later.

Tags: MySQL sql database Query SELECT

Posted on Fri, 08 May 2026 07:50:17 +0000 by universelittle