The SQL command used to retrieve data is SELECT. Every data read starts with it: you name the columns you want, the table they live in, and any filters or ordering you need, then the database returns a result set you can read, export or feed into an application. A minimal query looks like SELECT first_name, email FROM customers; - and if you only want a quick look at everything in a table, SELECT * FROM customers; returns every column. Before you can run either, you need access to a database (SQLite, PostgreSQL, MySQL/MariaDB, SQL Server or an equivalent), a way to send queries (a client like a database GUI, the vendor's CLI tool, or a query console in a cloud dashboard) and a user account with permission to read the tables involved. Nothing else is required to start; the rest of this guide is about pointing SELECT at the right rows.

What You'll Need

  • A working database connection - either a local file or server you control, or credentials for a server someone else administers.
  • A query tool: for SQLite, the sqlite3 command line; for PostgreSQL, psql; for MySQL/MariaDB, the mysql client; for SQL Server, Azure Data Studio or SQL Server Management Studio. Any of these also works: a GUI such as DBeaver or the query editor built into your hosting or cloud panel.
  • The exact table and column names. Use SELECT * on a table, or the schema browser in your client, to confirm them before writing anything complex.
  • Read permission on the schema or table. Without it you will get a permission error, not data.

Labels and menus differ slightly between clients, versions and hosting panels, so treat any button name below as a signpost rather than a guarantee.

Step-by-Step

  1. Open a query window. Connect with your client, then either type into the interactive prompt or open a new query tab. In GUI tools this is usually a toolbar button labelled "New Query" or similar, next to the connected database name.
  2. Choose the database or schema. Server databases hold several schemas. In psql you can switch with SET search_path TO myschema; or prefix tables as myschema.customers. In MySQL and SQL Server use USE database_name; (SQL Server: USE db). SQLite has no schemas, so skip this step.
  3. List the tables so you know what exists: SHOW TABLES; works in MySQL/MariaDB; \dt in psql; .tables in sqlite3; SELECT name FROM sqlite_master WHERE type='table'; also works for SQLite; and SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES; is portable across most server databases.
  4. Inspect the columns. Use DESCRIBE customers; (MySQL), \d customers (psql), PRAGMA table_info(customers); (SQLite) or query INFORMATION_SCHEMA.COLUMNS. Confirm the spelling and case of every column you plan to filter or sort on.
  5. Write the basic retrieval. SELECT first_name, last_name FROM customers; returns just those columns; SELECT * FROM customers; returns all of them. Prefer naming columns explicitly in anything you will keep, because * breaks quietly when a table gains columns.
  6. Add a filter with WHERE. SELECT first_name, last_name FROM customers WHERE country = 'PT'; limits the rows. Combine conditions with AND/OR, use IN ('PT','ES') for lists, BETWEEN 10 AND 50 for ranges, and LIKE 'ann%' for pattern matching. Wrap OR conditions in parentheses when mixing with AND.
  7. Test for missing values. NULL needs IS NULL or IS NOT NULL - WHERE email = NULL matches nothing, even when rows hold NULL.
  8. Sort and de-duplicate. ORDER BY last_name ASC, first_name ASC gives predictable order; add DESC for descending. Use SELECT DISTINCT country FROM customers; to list unique values.
  9. Limit the rows. LIMIT 10 in PostgreSQL, MySQL and SQLite; SELECT TOP 10 ... in SQL Server. Always add a limit when exploring a big table - an unfiltered SELECT on millions of rows can flood your client or time out.
  10. Join related tables. SELECT o.id, c.email FROM orders o JOIN customers c ON c.id = o.customer_id; pulls data from two tables on a shared key. Qualify every column with its table alias to avoid ambiguity errors.
  11. Aggregate when you need summary figures. SELECT country, COUNT(*) AS n FROM customers GROUP BY country; gives counts per country. Filters on the aggregate itself go in HAVING, not WHERE.
  12. Run the query with the client's run button or by pressing Enter/; depending on your tool. Read the row count and the actual values, not just the success message.
  13. Export or reuse. If you need the result outside the database, many clients offer a copy-to-CSV or export option in the results grid. If the query is one you will repeat, save it in the client rather than retyping it.

Common Mistakes

  • Forgetting the semicolon in a multi-line client. Most CLIs will sit waiting for more input until you add ;.
  • Using double quotes for text. In standard SQL, strings use single quotes: 'PT'. Double quotes usually mean an identifier, so "PT" may error out.
  • Comparing to NULL with =. As above, use IS NULL.
  • Running SELECT * on a large table without a limit, then wondering why the client hangs.
  • Mismatched case in quoted identifiers. PostgreSQL folds unquoted names to lower case; "CustomerName" and customername are different names.
  • Assuming the query changed data. SELECT only reads - if the numbers look wrong, check you connected to the right database before editing the query
  • Joining without a condition, which produces a cross product of every row in both tables and a huge, meaningless result.

Tips

  • Run a SELECT with a WHERE clause first to confirm which rows a filter reaches, then reuse that exact clause as the condition of an UPDATE or DELETE later.
  • Format queries with line breaks per clause (SELECT, FROM, WHERE, ORDER BY). It makes missing conditions obvious.
  • Use table aliases (customers c) once a query has more than one table.
  • Check ORDER BY plus LIMIT together whenever "the top rows" matter - without ordering, which rows you get is effectively arbitrary.
  • Keep exploratory reads on a read-only account where one exists, so a stray statement cannot change data.

Troubleshooting

"relation" or "table does not exist". Check the exact name with the table-listing query in step 3, confirm you selected the right database or schema, and remember unquoted names are case-insensitive in most engines.

"column does not exist". Re-check spelling with the describe command, and if two joined tables share a column name, prefix it with the alias.

Permission denied. Your user lacks read rights on that table. Ask the database administrator to grant them; you cannot fix this from the query side.

Empty result when you expect rows. Remove conditions one at a time to find the culprit, check for typos and leading or trailing spaces, and test IS NULL. If the table is genuinely empty, the query is correct.

Query runs forever or the client freezes. Cancel it in your client, then add a WHERE filter, a LIMIT, or fewer joined tables. Very large exports should go through the client's export tool rather than a giant result grid.

Connection refused or timed out. Restart the client, confirm the server is running and reachable, and verify the host, port and credentials. For a hosted database, check that your client's IP is allowed by the provider's firewall rules.

Frequently Asked Questions

Is SELECT the only command that retrieves data?

For reading rows, yes - SELECT is the standard command. Some databases add read-only helpers (metadata queries against INFORMATION_SCHEMA, or the SHOW shortcuts), but they all ultimately return result sets using SELECT or an equivalent table command.

What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping; HAVING filters groups after GROUP BY. Use HAVING COUNT(*) > 5, not WHERE COUNT(*) > 5.

Does SELECT change or delete any data?

No. A plain SELECT is read-only. Only statements like INSERT, UPDATE, DELETE, DROP or TRUNCATE modify data.

How do I retrieve only the first few rows?

LIMIT n in PostgreSQL, MySQL, MariaDB and SQLite, and SELECT TOP n ... in SQL Server. Combine it with ORDER BY so the rows you get are the ones you expect.

Can I get unique values only?

Yes - put DISTINCT straight after SELECT, as in SELECT DISTINCT country FROM customers;.

Why do I get duplicate rows after a join?

Usually the join matches several rows on one side - for example, one customer with many orders. Either add a filter, aggregate with GROUP BY, or check whether the key you joined on is actually unique.