SELECT retrieves rows and columns from one or more MariaDB tables based on optional filtering, sorting, grouping, and limiting clauses.
SELECT returns data stored in tables. You can fetch every column or only the fields you need, apply conditions, join tables, aggregate results, and order or limit the output.
List the column names after SELECT. Example: SELECT id, name, email FROM Customers;
This reduces network traffic and speeds up queries.
Attach a WHERE clause.Example: SELECT * FROM Orders WHERE total_amount > 100;
Use comparison, BETWEEN, IN, LIKE, IS NULL, and logical operators.
Use INNER JOIN (default), LEFT JOIN, RIGHT JOIN, or CROSS JOIN. Example: SELECT c.name, o.order_date FROM Customers c JOIN Orders o ON c.id = o.customer_id;
Apply GROUP BY with aggregate functions to summarize data.Example: SELECT customer_id, SUM(total_amount) AS lifetime_spend FROM Orders GROUP BY customer_id;
ORDER BY arranges rows; LIMIT restricts row count. Example: SELECT * FROM Products ORDER BY price DESC LIMIT 5;
Always specify columns, filter early, index filter columns, avoid SELECT *, test LIMIT before heavy queries, and analyze EXPLAIN plans.
Yes.Subquery example: SELECT name FROM Products WHERE id IN (SELECT product_id FROM OrderItems WHERE quantity > 10);
CTE example (MariaDB 10.2+): WITH high_value AS (SELECT id FROM Orders WHERE total_amount > 500) SELECT * FROM high_value;
Fetch the five most expensive items ever ordered, with buyer email: SELECT p.name, p.price, c.email FROM OrderItems oi JOIN Products p ON oi.product_id = p.id JOIN Orders o ON oi.order_id = o.id JOIN Customers c ON o.customer_id = c.id ORDER BY p.price DESC LIMIT 5;
.
Yes, Common Table Expressions (WITH) are available from MariaDB 10.2 onward and help structure complex queries.
Use LIMIT and OFFSET: SELECT * FROM Orders ORDER BY id LIMIT 20 OFFSET 40;
returns rows 41-60.
Performance is usually similar; DISTINCT is syntactic sugar for GROUP BY on the selected columns. Always test with EXPLAIN.