If you've ever needed to pull a specific report from your company's database and had to wait two days for a developer to write a SQL query for it, this guide is for you.
A database query is just a request for information. You're asking the database: "Give me all orders placed in the last 30 days" or "Show me customers who signed up but never made a purchase." The challenge has always been that databases only speak SQL — a formal query language that most people outside engineering don't know.
This guide explains how database queries work, shows you what SQL looks like, and explains why there's now a better way for most people to get answers from their data.
What Is a Database Query, Actually?
A query is an instruction to a database management system (DBMS) to retrieve, filter, sort, or manipulate data. Every time you search for something in your company's CRM, run a report in your analytics tool, or export a spreadsheet from your billing system — a query is running behind the scenes.
There are different types of queries:
For most business users, you'll almost always need SELECT queries and aggregations. The tricky part is writing them correctly in SQL syntax.
The Basics of SQL: What You're Actually Writing
SQL (Structured Query Language) has been the standard language for relational databases since the 1970s. PostgreSQL, MySQL, Microsoft SQL Server, and SQLite all use it. Here's what it looks like:
SELECT customer_name, email, total_spend
FROM customers
WHERE total_spend > 500
AND signup_date >= '2025-01-01'
ORDER BY total_spend DESC
LIMIT 50;This query does the following:
customer_name, email, and total_spend columnscustomers tableThe structure is logical once you understand it, but SQL requires exact syntax. A missing comma or a wrong table name throws an error. You also need to know the names of the tables and columns in your specific database — there's no built-in way to guess them.
How to Query a Database Step by Step (Traditional Method)
If you have database access and need to write queries yourself, here's the standard process:
1. Connect to the database
You'll need a database client like DBeaver, TablePlus, or psql (for PostgreSQL). You'll need the host address, port, username, password, and database name from whoever manages your infrastructure.
2. Understand the schema
Before you can query, you need to know what tables exist and what columns they have. In PostgreSQL, you can run:
-- See all tables in the database
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public';
-- See all columns in a specific table
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'orders';3. Start with simple SELECTs
Before filtering or joining, make sure you're pulling from the right table:
SELECT * FROM orders LIMIT 10;This shows you the first 10 rows so you can verify the structure and column names.
4. Add filters with WHERE
SELECT order_id, customer_id, amount, created_at
FROM orders
WHERE status = 'completed'
AND created_at >= NOW() - INTERVAL '30 days';5. Aggregate with GROUP BY
To answer "how many orders per country last month":
SELECT shipping_country, COUNT(*) AS order_count, SUM(amount) AS revenue
FROM orders
WHERE created_at >= NOW() - INTERVAL '30 days'
GROUP BY shipping_country
ORDER BY revenue DESC;6. JOIN tables for richer answers
Most real questions require combining tables. For example, to get the customer name alongside their orders:
SELECT c.name, c.email, o.order_id, o.amount
FROM orders o
JOIN customers c ON o.customer_id = c.id
WHERE o.status = 'completed'
ORDER BY o.created_at DESC
LIMIT 100;Common Mistakes When Querying Databases
Even developers make these errors regularly:
Using SELECT * in production — pulling every column from a large table is slow and wasteful. Always name the columns you actually need.
Forgetting WHERE clauses — an UPDATE or DELETE without a WHERE clause will affect every row in the table. Always double-check before running data-modification queries.
Off-by-one date ranges — time-based filters are tricky. >= '2026-01-01' and > '2025-12-31' look equivalent but behave differently with timestamps that include time.
Not understanding NULL — in SQL, WHERE column != 'value' does not return rows where the column is NULL. You need WHERE (column != 'value' OR column IS NULL).
Forgetting to LIMIT during exploration — when you're learning a new database, always add LIMIT 100 or similar to avoid accidentally returning millions of rows.
NoSQL Databases: A Different World
If your database is MongoDB, DynamoDB, or Firestore, the querying syntax is completely different. MongoDB uses JSON-like query objects instead of SQL:
// MongoDB: find completed orders from the last 30 days
db.orders.find({
status: "completed",
created_at: { $gte: new Date(Date.now() - 30 * 24 * 60 * 60 * 1000) }
}).sort({ amount: -1 }).limit(50)This produces the same kind of result as the SQL example above, but the language is entirely different. If you work across both relational and NoSQL databases, you're effectively learning two separate querying systems.
The Alternative: Asking Your Database in Plain English
Writing SQL takes practice and schema knowledge that most people don't have. That's why a new category of tools has emerged: natural language database interfaces that let you describe what you want and handle the SQL for you.
Tools like AI for Database work by connecting directly to your database, understanding its structure, and translating plain-English questions into SQL queries automatically. You type "show me revenue by country for the last 30 days, sorted highest to lowest" and get a table back — no SQL required.
The difference from using ChatGPT to write SQL is that the full loop is handled: schema discovery, query generation, execution, and result formatting all happen in one step. You don't need to copy SQL into a database client, manage credentials separately, or interpret raw query results.
This approach is particularly useful for: