How to Run an SQL Query
What you’ll build or solve
You’ll connect to a database and execute an existing SQL query using a graphical tool or command line. .
When this approach works best
This approach works best when:
Learn SQL on Mimo
- You want to test a query during development.
- You need to run a query directly against a database.
- You are executing a script in staging or production.
This is not needed if your application already runs queries internally and you don’t have direct database access.
Prerequisites
- Database credentials (host, port, username, password)
- Access to a SQL database such as MySQL, PostgreSQL, SQLite, or SQL Server
- An SQL query ready to execute
- A database client or terminal access
Step-by-step instructions
Step 1: Run queries interactively
You can run queries directly inside a database client.
Option A: Use a graphical client
Examples include DBeaver, MySQL Workbench, or pgAdmin.
- Open your database connection.
- Paste your SQL query into the editor.
- Click Run or Execute.
Example query:
SELECT id, name
FROM users
WHERE active=1;
The results appear in a results panel below the editor.
If multiple queries exist in the file, highlight the specific one before running.
Option B: Use the command line
First connect to the database.
MySQL:
CSS
mysql-u username-p-h localhost database_name
PostgreSQL:
psql-U username-h localhost-d database_name
SQLite:
sqlite3 database.db
Once connected, type your query and end it with a semicolon:
SELECT id, nameFROM usersWHERE active=1;
Press Enter to execute.
What to look for
- In terminal environments, queries must end with a semicolon.
- If the prompt changes to something like
mysql>ordatabase_name=#, you are connected. - If nothing runs after pressing Enter, the query likely lacks a semicolon.
Step 2: Run queries from a file
For longer scripts, you can execute a .sql file.
Option A: Run from your system shell
MySQL:
CSS
mysql-u username-p database_name < script.sql
PostgreSQL:
psql-U username-d database_name-f script.sql
This runs every statement inside the file.
Option B: Run from inside the database shell
MySQL:
CSS
SOURCE script.sql;
PostgreSQL:
CSS
\i script.sql
This executes the file while you are connected to the database.
What to look for
- If execution stops, the client usually reports the line number with the error.
- Make sure the file contains valid SQL statements separated by semicolons.
- Confirm you are connected to the correct database before running large scripts.
Examples you can copy
Example 1: Run a simple SELECT in the terminal
Bash
SELECT id, name
FROM users
LIMIT5;
Type this inside your connected SQL shell and press Enter.
Example 2: Run a filtered query in a GUI tool
SQL
SELECT id, total
FROM orders
WHERE total>100
ORDERBY totalDESC;
Paste into your editor and click Run.
Example 3: Execute a script file
From your system terminal:
psql-U username-d database_name-f reports.sql
This runs all queries inside reports.sql.
Common mistakes and how to fix them
Mistake 1: Forgetting the semicolon
You might type:
CSS
SELECT*FROM users
Why it breaks:
Many SQL shells wait for a semicolon before executing.
Correct approach:
CSS
SELECT*FROM users;
Mistake 2: Running against the wrong database
You might get an error like “table does not exist.”
Why it breaks:
You are connected to a different database than expected.
Correct approach in PostgreSQL:
\c correct_database_name
In MySQL:
CSS
USE correct_database_name;
Then rerun the query.
Mistake 3: Executing only part of a query in a GUI
You might highlight part of the statement.
Why it breaks:
The database receives incomplete SQL and throws a syntax error.
Correct approach:
Highlight the full query or place your cursor inside a complete statement before executing.
Troubleshooting
If you see “command not found,” confirm the database client is installed and added to your PATH.
If authentication fails, verify host, port, username, and password.
If a script fails mid-run, check the reported line number for syntax errors.
If nothing executes in the terminal, check for a missing semicolon.
If tables are missing, confirm the active database.
Quick recap
- Connect to your database using a GUI or CLI.
- Run queries interactively by executing full statements.
- End queries with a semicolon in terminal environments.
- Run larger scripts from a
.sqlfile. - Fix connection, database, or syntax issues before retrying.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot