Skip to content
BENIBIX Learn

Database Query

Development🗃️ DatabasesDatabase Basics

Also known as Query, SQL Query

A request sent to a database. It reads some data or changes it.

Think of it like

Think of it like a question you hand to a librarian. They go to the shelves and come back with the answer.

Example

Jack's admin page runs one query to list today's orders. It returns 120 rows in 8 milliseconds.

Why it matters

Every screen that shows data runs queries behind it. Their speed is the app's speed.

Definition

A query is a request you send to a database, either to read data or to change it. A read query asks for rows that match some conditions, like all orders over 50 euros. Others add, change or delete rows. The database plans how to answer each query, using indexes if it can. A slow query is one of the commonest reasons an app feels slow.

Example code

sql
SELECT name, COUNT(orders.id)
FROM customers
LEFT JOIN orders ON orders.customer_id = customers.id
GROUP BY customers.id;
-- Emma | 2
-- Jack | 1
-- Lucy | 0
How many orders each customer has, even the ones with none.

Common mistake

Asking for every column with SELECT * when you need two. It moves far more data than needed.

Key takeaways

  1. 1
    Read or change. Questions and edits are both queries.
  2. 2
    Planned for speed. The database picks the route.
  3. 3
    Ask for less. Only the rows and columns you need.