Getting Data Guide

This guide explains the SELECT statement in detail, covering how to retrieve, filter, limit, and sort data from your MariaDB database.

This guide explains how to retrieve data from MariaDB using the SELECT statement, progressing from basic syntax to more involved queries. Learn to select specific columns, limit results, filter with WHERE, sort with ORDER BY, join tables, and use various helpful options and functions.

Setup: Creating and Populating Example Tables

To follow the examples, first create and populate the books and authors tables:

Basic Data Retrieval

Selecting All Columns:

Use * to select all columns from a table.

Output (example):

Selecting Specific Columns:

List the column names separated by commas.

Output (example):

Limiting the Number of Rows with LIMIT:

  • To get the first N rows:

    Output (example):

  • To get N rows starting from an offset (offset is 0-indexed):SQL

    Output (example, assuming only 3 more rows exist after offset 5):

Filtering and Ordering Results

Filtering with WHERE:

Use the WHERE clause to specify conditions for row selection.

Output (example):

Ordering with ORDER BY:

Use ORDER BY column_name [ASC|DESC] to sort the result set.

Output (example):

  • ASC (ascending) is the default order. DESC is for descending order.

  • You can order by multiple columns: ORDER BY col1 ASC, col2 DESC.

  • Clause Order: SELECT ... FROM ... WHERE ... ORDER BY ... LIMIT .... MariaDB generally processes WHERE, then ORDER BY, then LIMIT.

Working with Multiple Tables (JOINs) and Functions

Joining Tables:

Use JOIN to combine rows from two or more tables based on a related column.

Output (example):

  • Alternative JOIN syntax: ... JOIN authors ON books.author_id = authors.author_id .... For more on joins, see the JOIN Syntax documentation or a "Basic Joins Guide".

  • CONCAT(str1, str2, ...): Concatenates strings.

  • AS alias_name: Assigns an alias to an output column.

Pattern Matching with LIKE:

Use LIKE in the WHERE clause for pattern matching. % is a wildcard for zero or more characters.

Output (example, same as above if only Dostoevsky matches):

SELECT Statement Modifiers

Place these modifiers immediately after the SELECT keyword.

  • ALL vs DISTINCT:

    • ALL (default): Returns all rows that meet the criteria.

    • DISTINCT: Returns only unique rows for the selected columns. If multiple identical rows are found for the specified columns, only the first one is displayed.

    Output (example, showing one "Crime & Punishment"):

  • HIGH_PRIORITY:

    Gives the SELECT statement higher priority over concurrent data modification statements (use with caution as it can impact write performance).

    SQL

  • SQL_CALC_FOUND_ROWS and FOUND_ROWS():

    To find out how many rows a query would have returned without a LIMIT clause, use SQL_CALC_FOUND_ROWS in your SELECT statement, and then execute SELECT FOUND_ROWS(); immediately after.

    Output (example for the first query):

    Then, to get the total count:

    Output (example, if 6 Dostoevsky books in total):

    The value from FOUND_ROWS() is temporary and specific to the current session.

This page is licensed: CC BY-SA / Gnu FDL

Last updated

Was this helpful?