Backend Node.js Course · Level 3
SQL basic
By FrontendPro Editorial TeamUpdated 8/19/2026
1. SELECT, WHERE, ORDER BY
- Syntax:
SELECT column FROM table WHERE condition ORDER BY column [ASC|DESC]. - Common mistake: incorrect keyword order, such as writing
ORDER DESC BY totalinstead of the correctORDER BY total DESC(ASC/DESCgoes after the column name, not betweenORDERandBY).
2. JOIN
JOINwithout specifying a type defaults toINNER JOIN: it only returns rows when there is a match in both tables. Any row in either table that does not find a corresponding match is excluded from the result.
sql
SELECT customers.name AS customer_name, orders.total
FROM customers
JOIN orders ON customers.id = orders.customer_id;
LEFT JOIN: retains all rows from the left table (the table placed beforeLEFT JOIN), regardless of whether a match is found in the right table. If no match exists, the columns retrieved from the right table evaluate toNULL.
sql
SELECT customers.name AS customer_name, orders.total
FROM customers
LEFT JOIN orders ON customers.id = orders.customer_id;
- Use case: a report to "list all customers including those who have never placed an order" requires
LEFT JOIN; usingINNER JOINwould drop customers without orders.
3. GROUP BY, COUNT
sql
SELECT customers.name AS customer_name, COUNT(orders.id) AS number_order
FROM customers
LEFT JOIN orders ON customers.id = orders.customer_id
GROUP BY customers.id, customers.name;
- Most SQL engines require every non-aggregated column (outside functions like
COUNT()) to appear in theGROUP BYclause. COUNT()counts non-NULLvalues in the specified column. Therefore,COUNT(primary_key_column)(such asorders.id) is safer thanCOUNT(other_column), because primary keys are guaranteed non-null when a row actually exists, avoiding undercounting when other columns contain missingNULLvalues (unrelated to theLEFT JOIN).
4. UPDATE
sql
UPDATE orders
SET status = 'shipped'
WHERE orders.id = 10;
- The
WHEREclause requires extreme caution inUPDATE(as well asDELETE) queries: omittingWHEREapplies the modification across the entire table, making it an extremely common and dangerous operational mistake in production.
