Primii pași în SQL

Primul SELECT în „Kotomarket”

6 min
What you'll learn
  • să scrii și să rulezi prima ta interogare de forma SELECT * FROM products LIMIT 5
  • să controlezi cât de mare este rezultatul schimbând numărul de rânduri din LIMIT
  • să explici de ce SELECT este sigur: doar citește datele și nu modifică baza de date

Cea mai importantă interogare

QUERY deschide în fața ta consola arhivei — o fereastră chihlimbarie în întunericul compartimentului. Dincolo de ea, baza de date „Kotomarket” așteaptă tăcută: cumpărători, produse, comenzi și evenimente din comportamentul utilizatorilor, înghețate în 2024. Indiciul de la finalul -ului era limpede: începe cu SELECT.

Cea mai mare parte a lucrului în SQL începe cu o acțiune simplă: citești datele. Pentru asta există comanda SELECT — o întrebare la care baza de date răspunde cu date: „arată-mi ce ai”.

Să începem cu vitrina: tabelul products. Interogarea de mai jos cere: „returnează toate coloanele (*) din tabelul products, dar nu mai mult de 5 rânduri”. Aici LIMIT n este limita: îi spune bazei de date să returneze cel mult n rânduri (o luăm puțin înainte — îl vom analiza în detaliu, împreună cu sortarea, în Capitolul 2). Apasă Rulează și primele rânduri din vitrină, adormite de 160 de ani, vor apărea pe holopanou. Apoi schimbă 5 în 10: vei vedea imediat cum controlezi volumul rezultatului.

QUERY: Apasă fără teamă. SELECT doar citește; cu o interogare de citire nu strici arhiva. Am verificat. De multe ori.

Un tabel holografic cu produse prinde viață deasupra consolei arhivei după primul SELECT
Primul SELECT trezește vitrina: cinci rânduri din products apar pe holopanou, ieșind din întuneric.

The single most important query

QUERY unfolds the archive console in front of you — an amber window in the darkness of the bay. Behind it the "Kotomarket" database sits in silence: buyers, products, orders and behavioral events, frozen since 2024. The hint at the end of the was clear: start with SELECT.

Most work with SQL really does begin with one simple act: read the data. That’s what the SELECT command is for — a question the database answers with data: "show me what you’ve got."

Let’s start with the storefront and peek into the products table. The query below asks: "return every column (*) from the products table, but no more than 5 rows." Here LIMIT n is the limiter: it tells the database to return at most n rows (looking ahead — we’ll dig into it properly, together with sorting, in Chapter 2). Hit Run, and the first rows of the storefront, asleep for 160 years, will come alive on the holopanel. Then change 5 to 10 — and you’ll see right away how to control how much the result returns.

QUERY: Go ahead, hit it. SELECT only reads — you can’t spoil the archive with a read query. I’ve tested that. Many times over.

Primele 5 produse din vitrina „Kotomarket”, după 160 de ani de tăcere:
Query result
idnamecategorypricestock
1Беспроводные наушникиЭлектроника5990.00120
2Смарт-часыЭлектроника8990.0060
3Портативная колонкаЭлектроника3490.0090
4Веб-камера HDЭлектроника2790.0045
5Механическая клавиатураЭлектроника4590.0070
Check yourself
Ce face cuvântul-cheie SELECT?