SQL
You might say
How do I write a query for users who placed an order in the last seven days?
Ask a relational database to read or change structured recordsSQL can select, filter, join, insert, update, and delete data in relational databases. Parameterize user values instead of building query strings, and inspect indexes and execution plans when a query becomes slow. A correct query still needs authorization around it.
Anatomy
SELECT name FROM users WHERE city = ?idnamecity
1MiaShenzhen
SELECT, INSERT, UPDATE or DELETE, decide whether to read or modify
Relational data collection to be operated; first confirm the table name and field structure
WHERE limits the scope of influence; placeholders such as question marks are safely passed by parameterized queries.
Variants
SELECT
SELECTRead matching rows
Read records without changing them.
INSERT
INSERTAdd a new row
Create a new record.
UPDATE
UPDATEChange the row hit by WHERE
Update selected records; check the WHERE clause.
DELETE
DELETEDelete the row hit by WHERE
Remove selected records after checking recovery.
Typical use cases
Order query
Query usersSELECT to find records that meet the conditions
SELECT id, name, planFROM usersWHERE plan = 'pro';
Result: u_23 · Alex Chen · pro
User lookup
New taskINSERT creates a record
ExecuteINSERT INTO tasks ...
→
Returnid = task_8842
Analytics total
Update dataUPDATE only modifies the specified user
UPDATE usersSET city = 'Shenzhen'WHERE id = 'u_23';
Affects 1 row · Modification successful
Data update
Delete recordConfirm WHERE before DELETE
SELECT WHERE id=t_42→Confirm 1 line→DELETE
Avoid accidentally deleting the entire table
SQL vs NoSQL: which fits your data?
SQL is a query language; the actual choice is between a relational database and a specific NoSQL database. Compare them by data relationships, access patterns, and consistency needs—not by assuming one is faster.
| Decision point | Relational (SQL) | NoSQL database |
|---|---|---|
| Data structure | Stores structured records in tables and connects related data through fields. | Uses models such as document, key-value, wide-column, or graph. |
| Decision point | Often the direct fit when joins, constraints, and transactions matter. | Can fit a well-defined data model and access pattern with a purpose-built product. |
| Typical fit | Orders, accounts, and other related business records that need consistency. | Key-value caches, documents, event records, or graph relationships. |
Further reading