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.
Know first
SELECT name, city FROM users WHERE city = 'Shenzhen'
idnamecity
1XiaoliShenzhen
2AjiHangzhou
3Xiao LinShanghai

When to use it

  • Filter and sort records
    Learn to read data first
    QuerySELECT * FROM users WHERE id = 42 Result42 · Jordan
  • Join related tables
    Preview the impact before deleting or changing data
    SELECT ... WHERE id=42Confirm 1 rowDELETE
  • Aggregate totals and counts
    Treat input only as data
    SELECT * FROM users WHERE email = $1$1 = "oil@example.com"
    The parameter is not executed as another piece of SQL.
  • Update data inside a transaction
    Give AI the table schema too
    users.iduuidusers.emailtext · uniqueOutputExplain how many rows are affected

When NOT to use it

  • Concatenate untrusted input into a query
    Deletion without WHERE
    RunDELETE FROM users Impact12,480 rows deleted
  • Select every column when only a few are needed
    Concatenate input directly into SQL
    email = ' OR 1=1 --SELECT * FROM users WHERE email = '' OR 1=1
  • Run an unbounded update or delete
    Run a write operation in production without confirming the conditions
    Current environmentPRODUCTIONAbout to runUPDATE users ...
    Verify first with test data or in a transaction
  • Assume a database permission replaces application authorization
    Display text cannot replace stable fields
    Store only"Premium member" Should beplan_id: pro_2026
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_42Confirm 1 lineDELETE
Avoid accidentally deleting the entire table
Quick decision

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 pointRelational (SQL)NoSQL database
Data structureStores structured records in tables and connects related data through fields.Uses models such as document, key-value, wide-column, or graph.
Decision pointOften 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 fitOrders, accounts, and other related business records that need consistency.Key-value caches, documents, event records, or graph relationships.
Further reading