SQL Basics

Key points

  • SQL is a language for telling a database to "find," "add," "rewrite," or "delete" data.
  • The four basic commands are "SELECT," "INSERT," "UPDATE," and "DELETE."
  • Rather than memorizing detailed syntax up front, what matters most is grasping what each command does.

What is SQL?

SQL (Structured Query Language) is a dedicated language for manipulating data stored in a database. An application builds SQL statements based on what happens on screen and sends them to the database.

Action in
the app
Converted to SQL
Sent to the
database
Result returned
Shown on
screen

Teacher Pochi's hintThis resembles the words you use to order at a shop. Phrases like "I'll take this" or "please change this one" — using set phrasing to state what you want gets you exactly the response you're after.

SELECT: finding data

SELECT is the command for searching a table and retrieving data. You specify which table, which columns, and under what conditions to retrieve.

SELECT
Find rows matching
the condition
Retrieve the
matching data

Teacher Pochi's hintThis resembles searching a library's book catalog. You enter conditions like "author" or "title" and press search, and a list of matching books appears — that action is exactly what SELECT does.

INSERT: adding data

INSERT is the command for adding a new row (record) to a table. It runs behind the scenes of operations like signing up a new member or placing a new order — anything that "creates something new."

Teacher Pochi's hintPicture a library receiving a newly arrived book and adding one new line to its book catalog. That act of "adding one line to the ledger" is what INSERT does.

UPDATE: rewriting data

UPDATE is the command for rewriting the contents of data that already exists. You specify which row, which column, and what new value to change it to, then run it.

Teacher Pochi's hintPicture a library member moving to a new address: instead of touching the book catalog, the librarian opens that member's page in the patron register and rewrites just the address field. That's UPDATE.

DELETE: deleting data

DELETE is the command for removing a row from a table that's no longer needed. It's often used behind the scenes of a cancellation operation, and forgetting to specify a condition can accidentally delete far more than intended, so care is required.

Teacher Pochi's hintPicture erasing exactly one line from the patron register for someone who has left the library. If you erase the wrong range, other members' information could get wiped out too — so this calls for careful handling.

Summing up the four basic commands

SELECT, INSERT, UPDATE, and DELETE are the four basic commands for database operations, sometimes referred to by the acronym "CRUD."

SELECTFind (read)
INSERTAdd (create)
UPDATERewrite (update)
DELETEDelete (delete)

Once you have a solid grasp of what each of these four commands does, you'll be able to infer what's happening to the database when you read a real system's code or logs.

Practical pitfalls to watch for

Forgetting the WHERE clauseOmit the WHERE clause on an UPDATE or DELETE and the entire table becomes the target.
Insufficient protection against SQL injectionConcatenating user input straight into a SQL string lets malicious input rewrite the statement. Using placeholders (prepared statements) is the standard defense.
Check with SELECT before running the real thingRunning the same condition as a SELECT before an UPDATE or DELETE lets you confirm the target rows first and avoid accidents.

Summary

SQL is the language for operating on a database, and its four basic commands — SELECT, INSERT, UPDATE, and DELETE — handle searching, adding, updating, and deleting data. Detailed syntax can be looked up whenever you need it; what matters first is understanding each command's role.

Related topics:

🏠 Back to top