Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Data Collection  ›  Lesson

Collecting Data from Databases

Databases 12 minDatabases store an organisation's data in related tables, and analysts pull exactly the rows and columns they need with a SQL query.

Collecting Data from Databases

Kettle & Crumb's till does not save a spreadsheet. Every sale goes straight into a database, alongside the menu and the loyalty list. When Nisha asks for June's cold-coffee sales, the analyst's first job is to get exactly those rows out, reliably, and in a way that can be repeated next month.

The rule

Ask the database for exactly the rows and columns you need.

A relational database stores data in tables that link to each other through keys:

  • orders(order_id, order_date, customer_id, item_code, qty, amount)
  • menu(item_code, item, price)
  • customers(customer_id, name, joined)

orders.item_code points to menu.item_code, so item names are stored once in menu rather than copied into every order. You pull data out with SQL, a language for asking questions of tables:

SELECT order_date, item_code, qty, amount
FROM orders
WHERE item_code = 'C01'
  AND order_date >= '2025-06-01'
  AND order_date <  '2025-07-01';
  • SELECT names the columns you want.
  • FROM names the table.
  • WHERE keeps only the rows that pass every condition.

When you need a name from another table, a JOIN matches rows on their shared key, just as VLOOKUP did on a sheet:

SELECT o.order_date, m.item, o.qty
FROM orders AS o
JOIN menu AS m ON m.item_code = o.item_code;

Analysts normally get read-only access, often to a copy of the live database, so a mistake cannot damage the till's records.

The shortcut

Before you write the query, fill in three blanks: "I need these columns, from this table, for rows where this is true." They become SELECT, FROM and WHERE, in that order.

"Cold-coffee orders in June"Step 1 of 4
  1. Columns

    Date, quantity and amount: SELECT order_date, qty, amount.

The trap is exporting a whole table "to be safe" and filtering by hand. It is slow, it leaves no record of what you kept, and it is easy to get subtly wrong.

Drills

Answer fast — trust the rule.

quick drill

Which clause keeps only the rows you want?

quick drill

Why is the item name stored in menu rather than in every order?

quick drill

Which query returns every order from Asha, whose customer_id is 7?

quick drill

Why does the query use order_date < '2025-07-01' rather than <= '2025-06-30'?

Remember

  • Ask the database for exactly the rows and columns you need.
  • Shortcut: columns, table, condition become SELECT, FROM, WHERE.
  • Trap: exporting everything and filtering by hand; save the query instead.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPull only what you need

Write a SQL query that returns the order date, item code and amount of every order in June 2025 from the orders table, and explain why pulling only these rows is better than exporting the whole table.