SQL for Beginners: Queries Every Developer Should Know
SQL is the language used to talk to databases. Almost every app, from small shops to banks, stores data in a database, so SQL is a skill that never goes out of date.
The four core commands
SELECT reads data, INSERT adds new rows, UPDATE changes existing rows and DELETE removes them. Always use a WHERE clause with UPDATE and DELETE, otherwise you will change every row in the table.
Filtering and sorting
Use WHERE to filter, ORDER BY to sort and LIMIT to restrict the number of results. Combine them to answer questions such as which five customers spent the most this month.
Joins and grouping
JOIN connects tables through a shared column, and GROUP BY with SUM or COUNT builds reports. Practise on a free database such as SQLite or MySQL and write at least one new query every day.
Understanding tables, rows and columns
A database stores information in tables. Each table is like a spreadsheet: columns describe the type of information, such as name, price or date, and every row is one record. A shop might have a products table, a customers table and an orders table. Good design keeps each kind of information in its own table so nothing is repeated, which makes data easier to update and less likely to contain mistakes.
Every table should have a primary key, a unique number that identifies each row. Other tables point to it with a foreign key. For example, the orders table stores the customer id instead of the full customer details. This link is what makes joins possible.
Useful queries to practise
Start by reading data with SELECT and a few columns. Then filter it: find all products cheaper than a certain price, all customers from one city, or all orders from the last thirty days. Add ORDER BY to sort by price or date, and LIMIT to show only the first ten rows. These simple patterns answer a large share of real business questions.
Next, learn the aggregate functions COUNT, SUM, AVG, MIN and MAX. Combined with GROUP BY, they produce reports such as sales per month, average marks per class or the number of orders per customer. Use HAVING to filter the grouped results, for example only customers who placed more than five orders.
Joins explained simply
A join combines rows from two tables. An INNER JOIN returns only the rows that match in both tables, such as orders that have a customer. A LEFT JOIN returns every row from the first table even when there is no match, which is useful for finding customers who have never ordered. Beginners often find joins hard, so draw the two tables on paper and connect the matching columns with a line before writing the query.
Mistakes and safety
Always run a SELECT with your WHERE condition first to see which rows would be affected, and only then change it to UPDATE or DELETE. Take a backup before big changes. Never build queries by joining user input into text, because that opens the door to SQL injection; use parameters or prepared statements instead. Also avoid SELECT star in production code, since asking for only the columns you need is faster.
Speeding up slow queries
If a query is slow, the table probably needs an index on the column you search or join on. An index works like the index of a book: the database jumps straight to the right place instead of reading every row. Do not add indexes everywhere, because they slow down inserts. Use the EXPLAIN command to see how the database runs your query and fix the slowest step first.
Where to go next
Once you are comfortable, learn about normalisation, transactions and views. Then connect a database to a real project, for example a Python script or a small website that stores contacts. Building something real turns memorised commands into lasting skill.