Filtering, Sorting, Functions, Subqueries, and Useful Query Patterns
Move beyond basic SELECT statements and learn the query patterns used in dashboards, search screens, and back-office tools.
Inside this chapter
- Filtering for Business Questions
- Ordering and Limiting Results
- Useful Built-In Functions
- Subqueries and Reusable Thinking
Series navigation
Study the chapters in order for the smoothest path from relational foundations to production-level MariaDB operations. Use the navigation at the bottom of each page to move chapter by chapter through the full series.
Filtering for Business Questions
Most useful queries do not read every row. They answer a question such as: which orders are still pending, which customers joined this month, which products are out of stock, or which invoices exceed a threshold. SQL filtering lets you ask those targeted questions directly.
SELECT order_id, customer_id, order_status
FROM orders
WHERE order_status IN ('PENDING', 'PACKING')
AND order_date >= '2026-01-01'; Ordering and Limiting Results
SELECT product_name, unit_price
FROM products
WHERE is_active = 1
ORDER BY unit_price DESC
LIMIT 10;
This pattern is common in leaderboards, report previews, admin dashboards, and API endpoints. Without a clear ordering rule, paginated results may become inconsistent or confusing.
Useful Built-In Functions
MariaDB includes string, numeric, date, and conditional functions that let you shape query results without shifting everything into application code.
SELECT
full_name,
UPPER(status) AS normalized_status,
DATE(created_at) AS signup_date
FROM customers;
SELECT
product_name,
ROUND(unit_price * 1.18, 2) AS price_with_tax
FROM products; Subqueries and Reusable Thinking
SELECT customer_id, full_name
FROM customers
WHERE customer_id IN (
SELECT customer_id
FROM orders
WHERE order_status = 'PENDING'
);
Subqueries can help express business logic, but they should be used thoughtfully. Sometimes a join is more efficient or easier to read. Advanced SQL maturity means choosing the clearest and most performant form for the problem.