Introduction to SQL · Lesson 2 of 30
Install MySQL and Run Your First Query
To learn SQL properly you need to run queries yourself. This course uses MySQL 8, the most widely used open-source database. This lesson installs it, connects to it, creates a database and runs your first query – about fifteen minutes of setup that pays off for the rest of the course.
In this lesson
- Installing MySQL on Windows, macOS and Linux
- Connecting with MySQL Workbench or the command line
- Creating and selecting a database
- SQL syntax rules: statements, case and comments
Install MySQL
| System | How |
|---|---|
| Windows | Download MySQL Installer from dev.mysql.com/downloads, choose "Developer Default" (server + Workbench + shell), set a root password and remember it. |
| macOS | brew install mysql then brew services start mysql – or use the DMG installer from the same page. |
| Ubuntu / Debian | sudo apt install mysql-server then sudo mysql_secure_installation. |
| No install | Docker: docker run --name mysql -e MYSQL_ROOT_PASSWORD=secret -p 3306:3306 -d mysql:8 |
Everything in this course also works on MariaDB (a MySQL fork that XAMPP installs), and nearly everything works on PostgreSQL – differences are pointed out where they matter.
Connect
Option 1: MySQL Workbench (visual)
- Open Workbench and click the Local instance connection.
- Type SQL in the query tab and press Ctrl+Enter (run the current statement) or the lightning icon (run all).
- Results appear in a grid below.
Option 2: the command line
mysql -u root -p
# Enter password: ********
mysql> SELECT VERSION();
Other good clients: DBeaver (free, works with every database), TablePlus, and the VS Code "SQLTools" extension.
Create a database and run a query
CREATE DATABASE learnfrenzy_shop;
USE learnfrenzy_shop;
Now run your first query:
SELECT 'Hello, SQL!' AS greeting, 2 + 3 AS answer, CURRENT_DATE > '2020-01-01' AS is_after_2020;
+-------------+--------+---------------+ | greeting | answer | is_after_2020 | +-------------+--------+---------------+ | Hello, SQL! | 5 | 1 | +-------------+--------+---------------+
SELECT can calculate values even without a table. AS gives each result column a name (an alias). Comparisons return 1 for true and 0 for false in MySQL.
SHOW DATABASES; -- list databases
SHOW TABLES; -- list tables in the current database
DESCRIBE customers; -- show a table's columns (after lesson 3)
SQL syntax rules
-- A single-line comment
/* A multi-line
comment */
SELECT name, city -- keywords are case-insensitive…
FROM customers
WHERE city = 'Pune'; -- …but string values are compared by the column's collation
select NAME from CUSTOMERS where CITY = 'pune'; -- also works in MySQL, but hard to read
- A statement ends with a semicolon. You can split it over many lines.
- Write keywords in UPPER CASE and table/column names in lower case – the most common professional style.
- Strings use single quotes:
'Pune'. Double quotes and backticks are for identifiers in some databases. - In MySQL, string comparisons are usually case-insensitive (because of the default collation) –
'pune' = 'Pune'is true. In PostgreSQL they are case-sensitive.
Common setup problems
- Access denied for user 'root' – wrong password, or on Ubuntu use
sudo mysqlfirst. - Can't connect to MySQL server – the service isn't running; start it (
brew services start mysql, Services app on Windows). - No database selected – run
USE learnfrenzy_shop;or pick it in your client.
Practice
Try it yourself
Use one SELECT to show your name, today's date and 18% GST on ₹2,500.
Show solution
SELECT 'Asha' AS learner, CURRENT_DATE > '2025-01-01' AS date_ok, 2500 * 0.18 AS gst;+---------+---------+--------+ | learner | date_ok | gst | +---------+---------+--------+ | Asha | 1 | 450.00 | +---------+---------+--------+
Quick check
Which command switches to a database?
USE database_name;
Are SQL keywords case-sensitive?
No – SELECT and select are the same; upper-case keywords are just a readability convention.
Which quotes are used for text values?
Single quotes, e.g. 'Pune'.
Summary
- Install MySQL 8 (or use Docker/MariaDB) and connect with Workbench, DBeaver or the command line.
CREATE DATABASEandUSEset up your workspace;SELECTruns queries.- End statements with semicolons, use single quotes for strings, write keywords in upper case.
Finished this lesson?
Mark it complete to track your progress, then carry on with the next lesson.