Most people searching this want to pull data from a database: you write a SQL query using SELECT, FROM, and WHERE, in that order. If you meant a search-engine query or a database request in an app, the same planning steps apply, but the wording changes.
Time 15 min · Steps 7 · Level Easy
A query is a written request for data. The most common situation is asking a relational database a question in SQL, the language nearly every database system understands, and the method is the same everywhere: name the table, name the columns, filter the rows. The case where the method differs is when you are querying a search engine or a no-SQL tool, where you type plain keywords instead of code, covered at the end.
Write the Query in Seven Steps
- Write your question in plain words first. One sentence, one question: "show me all customers who signed up this month." A query answers exactly one question well.
- Find the table that holds the answer. Every database stores data in named tables such as customers or orders. Ask your database admin, or run a command that lists tables (in most systems, something like SELECT name FROM sqlite_master WHERE type='table', or the table list in your database tool's sidebar).
- Start with SELECT and the columns you want. Write SELECT first_name, last_name FROM customers. Listing specific columns beats SELECT * because the result is readable and the query runs faster on large tables.
- Filter rows with WHERE. Add WHERE signup_date >= '2025-03-01' to keep only the rows you care about. Text values need single quotes; numbers do not.
- Sort and limit the result. Add ORDER BY signup_date DESC to put newest first, and LIMIT 100 so a mistake returns 100 rows instead of millions. Some systems spell this TOP 100 or FETCH FIRST 100 ROWS ONLY.
- Run it on test data. Execute the query against a copy or a small table before pointing it at production. Read the first rows by hand to confirm they answer your original sentence.
- Save the working query with a comment. Add a line above it like -- customers who signed up in March, then store it in a file or your database tool's saved-queries area so you can reuse and edit it later.
The Five Clauses That Cover Most Queries
Almost every working query is built from the same small set of clauses, and knowing their fixed order is most of the skill. SQL runs them in this order even though you type them in the same order shown here:
- SELECT – which columns appear in the result.
- FROM – which table the rows come from.
- WHERE – which rows to keep, row by row.
- GROUP BY – collapse rows into groups, for counting or totalling.
- ORDER BY – the sort order of the final result.
A worked example combining them: SELECT country, COUNT(*) AS total FROM customers WHERE signup_date >= '2025-01-01' GROUP BY country ORDER BY total DESC. This returns one row per country with a count, newest-first countries at the top. The AS total part names the count column so you can sort by it.
When the Query Breaks: Symptom, Cause, Fix
| Symptom | Likely cause | Fix |
|---|---|---|
| Syntax error near a word | Missing comma, or a reserved word used as a column name | Check the line the error points to; rename columns like order or group to order_date, group_name |
| Zero rows returned | WHERE condition too strict or wrong date format | Remove the WHERE clause; if rows appear, re-add filters one at a time |
| Far too many rows | Missing WHERE, or a join doubling rows | Add LIMIT 100 while testing; check that joined tables match on unique keys |
| Column not found | Typo, or wrong table | List the table's columns (in most tools, a right-click or a DESCRIBE-style command shows them) |
| Text comparison returns nothing | Case mismatch or trailing spaces | Use WHERE LOWER(name) = LOWER('Ann') and TRIM(name) |
How to Tell the Query Worked
Run three checks before you trust any result. First, row count sanity: if you filtered to one month, the number of rows should be smaller than the whole table, not equal to it. Second, spot-check: pick two rows you can verify another way, such as a record you know personally, and confirm the values match. Third, edge cases: check that a row you deliberately excluded is really absent. A query that passes all three is safe to build on.
Before You Run Anything: a Five-Point Checklist
- You know which database and table you are querying, and whether it is live data or a copy.
- Your query has a LIMIT while you are still testing.
- Any UPDATE or DELETE statement has its WHERE clause written and double-checked first, because these change or remove data permanently.
- Text values are wrapped in single quotes, dates match your database's format.
- You have the plain-language sentence the query is supposed to answer, so you can check the result against it.
If You Meant a Search-Engine Query Instead
For search engines, a query is just the words you type, and small changes pull better results. Put the most important words first, use the specific noun rather than a vague phrase ("adjust bike rear derailleur" beats "bike problem"), and wrap an exact phrase in double quotes. Add a site name to search one place, and a minus sign to exclude a word, as in jaguar -car. Operators and menu labels can differ slightly between search engines and versions, so treat these as patterns rather than fixed rules.
Official source: W3Schools is a common reference, but the bodies that own the SQL standard itself are ISO and ANSI; for database-specific syntax, each vendor's own documentation is the authority.