Run your first SQL in the mysql client
35 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)The mysql client is a terminal program where you type SQL, press Enter and see the answer as a table. A few habits, such as ending every statement with a semicolon and reading the prompt, make it easy to use.
Connect with mysql -u root -p and read the prompt
The mysql client is the terminal program that sends your SQL to the MySQL server and prints the answer as a text table. Its prompt, mysql>, shows it is ready for a new statement.
Open Command Prompt (Windows) or Terminal (macOS) and run mysql -u root -p. -u root names the user, and -p makes the client ask for the password without showing it. After the welcome text you see the prompt mysql>: the client is waiting for SQL.
Example 1 · Connect and read the welcome text
mysql -u root -pEnter password: ******** Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 11 Server version: 8.4.6 MySQL Community Server - GPL Copyright (c) 2000, 2025, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
What this code does
- "Commands end with ; or \g" is the semicolon rule.
Server versionshows which MySQL you reached, and the connection id is just this session's number; both differ on your computer. - The last line is the prompt
mysql>. Everything you type after it goes to the server.
The prompt changes to tell you what the client is waiting for. Learn these; they explain most "nothing happens" moments:
->the statement is not finished yet: the client is waiting for more lines or for;.'>or">a quote was opened and never closed. Type the closing quote; to give up instead, type the quote and then\c.- To cancel any half-typed statement, type
\cand press Enter. To leave the client, typeexit.
SELECT VERSION(), NOW() and simple expressions
An alias is a name you give to a result column with AS, such as 45000 * 0.10 AS scholarship. Without an alias, the column is named after the expression itself.
SELECT asks the server for values. With no table it simply works out what you give it: SELECT 2 + 3; prints 5. Functions such as VERSION() and NOW() are built-in helpers that return a value; the brackets are required even when empty.
Example 2 · Ask the server about itself
SELECT VERSION(), NOW();+-----------+---------------------+ | VERSION() | NOW() | +-----------+---------------------+ | 8.4.6 | 2025-06-14 10:32:07 | +-----------+---------------------+
What this code does
VERSION()returns the server version andNOW()the current date and time on the server, inYYYY-MM-DD hh:mm:ssform.- This is a sample: your version number and the time will differ. One
SELECTcan return several values, separated by commas, as several columns.
Example 3 · Expressions, functions and aliases
SELECT 2 + 3, 7 * 8, 10 / 4, 10 DIV 4, 17 % 5;
SELECT 'Hello, MySQL' AS greeting, 45000 * 0.10 AS scholarship, CONCAT('Roll no ', 21) AS label;+-------+-------+--------+----------+--------+ | 2 + 3 | 7 * 8 | 10 / 4 | 10 DIV 4 | 17 % 5 | +-------+-------+--------+----------+--------+ | 5 | 56 | 2.5000 | 2 | 2 | +-------+-------+--------+----------+--------+ +--------------+-------------+------------+ | greeting | scholarship | label | +--------------+-------------+------------+ | Hello, MySQL | 4500.00 | Roll no 21 | +--------------+-------------+------------+
What this code does
/always gives a decimal answer (2.5000),DIVgives the whole-number part (2) and%the remainder (2). Without an alias, each column is named after its expression.- Text goes in single quotes.
CONCATjoins values into one text, andASnames each result column; oneSELECTstatement can span several lines.
After every result the client prints a line such as 1 row in set (0.00 sec): the number of rows and the time taken. The lessons leave that line out, because the time changes on every run.
Predict the output
Predict the answers
Read the query and pick what it prints.
Semicolons, case and multi-line statements
A statement is one complete SQL command. It ends at the semicolon ;, not at the end of the line, so until the client sees the ; it keeps waiting and shows the -> prompt.
A statement ends at the semicolon ;, not at the end of the line. So you can split a long query over several lines; the client shows -> until it sees the ;. Pressing Enter without one is the most common reason "nothing happens".
SELEC name FROM customers;ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELEC name FROM customers' at line 1
SQL keywords are not case-sensitive: select, SELECT and Select all work. Table and column names are safest written exactly as they were created, usually in lower case, because on some systems (Linux servers in particular) table names are case-sensitive. A mistyped keyword gives error 1064, the syntax error; the text after near shows where MySQL got confused.
Example 4 · The -> prompt, an open quote and \c
SELECT 10 + 20;SELECT 'Ravi'\cmysql> SELECT 10 + 20
-> ;
+---------+
| 10 + 20 |
+---------+
| 30 |
+---------+
1 row in set (0.00 sec)
mysql> SELECT 'Ravi
'> '\c
mysql>What this code does
- The first statement was sent without
;, so the client showed->and waited. Typing;on the next line finished it. - In the second, the quote after
SELECTwas never closed, so the prompt became'>: the client treats everything, even\c, as part of the text. Typing the closing'first and then\cthrew the statement away and brought backmysql>.
Example 5 · A multi-line query in lower case
select name, cityfrom customerswhere city = 'Delhi'order by name;+-------------+-------+ | name | city | +-------------+-------+ | Kabir Singh | Delhi | | Rahul Verma | Delhi | +-------------+-------+
What this code does
- Keywords work in any case, and line breaks are just spaces to MySQL. Most people still write keywords in capitals and put each clause on its own line, so queries are easy to read.
- This query uses the practice database, which you load two lessons from now; until then, try the same shape on the expressions above.
Fix the error
Fix the syntax error
Read the real error, then pick the line that fixes it.
List the customers by name.
Error
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELEC name, city FROM customers ORDER BY name' at line 1
SHOW DATABASES, USE, SHOW TABLES, DESCRIBE and exit
The current database is the one chosen with USE. Table names in later statements refer to it, so you can write customers instead of practice.customers.
One MySQL server holds many databases, and each database holds tables. SHOW DATABASES; lists them. A fresh install has four system databases that MySQL uses for itself: look, but never change them.
Example 6 · Look around a fresh server
SHOW DATABASES;USE mysql;SHOW TABLES LIKE 'time_zone%';DESCRIBE time_zone_name;+--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ +------------------------------+ | Tables_in_mysql (time_zone%) | +------------------------------+ | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | +------------------------------+ +--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | Name | char(64) | NO | PRI | NULL | | | Time_zone_id | int unsigned | NO | | NULL | | +--------------+--------------+------+-----+---------+-------+
What this code does
- A fresh server lists only the four system databases; yours will grow as you create your own.
USE mysql;switches to MySQL's own database (the client printsDatabase changed), andLIKE 'time_zone%'keeps only tables whose names start withtime_zone, since the full list is long. DESCRIBEshows the columns of one of those tables. Only look inside the system databases; changing them can break the server.
USE name; picks the current database; the client answers Database changed. SHOW TABLES; lists the tables in the current database and DESCRIBE table; lists a table's columns.
SHOW DATABASES, USE, SHOW TABLES and DESCRIBE are your "where am I and what is here" commands. Use them whenever you connect to a database you have not seen before. exit (or quit) closes the client and prints Bye.
The same in a MySQL Workbench query tab
MySQL Workbench is a free window-based tool for MySQL. Its query tab is a text editor for SQL, and each result appears in the Result Grid below it.
In Workbench, click your local connection on the home screen and type the root password. A query tab opens: a text editor for SQL.
SELECT 'Hello from Workbench' AS message, 12 * 5 AS answer;+----------------------+--------+ | message | answer | +----------------------+--------+ | Hello from Workbench | 60 | +----------------------+--------+
Type the statement, then run it; the result appears in the Result Grid below, and the Output panel at the bottom logs each statement with a green tick or a red cross and a message such as 1 row(s) returned.
Workbench runs either the statement under the cursor or every statement in the tab. On the left, the Navigator lists the databases (Workbench calls them schemas); double-click one to make it the current database, like USE. Open a new query tab with Ctrl+T (Cmd+T on macOS). Run the statement under the cursor with Ctrl+Enter (Cmd+Enter), or every statement in the tab with Ctrl+Shift+Enter (Cmd+Shift+Enter); the lightning-bolt button does the same.
Next step
Check your understanding
10 quick questions