By WebNest Studio Engineering Team
Database and SQL Tutorial
Setting Up a Local Database for Practice
Reading about SQL only gets you so far — actually running queries against a real database is what makes the syntax and concepts stick. This lesson walks through getting a working database on your own machine (or a free hosted alternative), connecting to it with either a GUI tool or the command-line client, and creating your first practice database and table.
Why it matters
SQL is learned by typing queries and reading what comes back, not by reading about it. A database you can experiment in freely, and break without consequences, is the most useful tool you can have while learning. The same create, insert and select cycle you run here is what every later lesson builds on.
Installing PostgreSQL or MySQL Locally
PostgreSQL and MySQL are the two most widely used open-source relational databases, and either is a fine choice for learning SQL — the core language is nearly identical between them, with only minor syntax differences. Download the installer for your operating system from the official PostgreSQL or MySQL site and run it; the installer typically also sets up a default admin user and asks you to set a password during setup. If you'd rather avoid a local install, hosted free-tier services (such as Supabase or Neon for PostgreSQL, or PlanetScale for MySQL) give you a real remote database in a couple of minutes, reachable with the same tools and the same SQL.
Connecting with a GUI Client or the CLI
A GUI database client shows your tables, columns, and data visually and is often friendlier while learning. Popular options include TablePlus, DBeaver, and pgAdmin (which ships alongside PostgreSQL). To connect, you supply a host (usually localhost for a local install), port (5432 for PostgreSQL, 3306 for MySQL by default), username, password, and database name. Alternatively, the command-line clients psql (PostgreSQL) and mysql (MySQL) let you type SQL directly in a terminal: psql -U postgres -d postgres or mysql -u root -p.
Creating Your First Practice Database and Table
Once connected, create a dedicated database to practice in — keeping it separate from any default system database avoids accidentally cluttering or breaking anything the tool itself relies on. Then create a simple table and insert a few rows to confirm everything works end to end.
Example
Creating a practice database and table in PostgreSQL via psql
-- Run inside psql after connecting
CREATE DATABASE practice_db;
\c practice_db
CREATE TABLE students (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
grade INT
);
INSERT INTO students (name, grade) VALUES ('Asha Patel', 92), ('Ravi Kumar', 85);
SELECT * FROM students;
You are now connected to database "practice_db" as user "postgres".
CREATE TABLE
INSERT 0 2
id | name | grade
----+-------------+-------
1 | Asha Patel | 92
2 | Ravi Kumar | 85
(2 rows)
Common Mistakes
- Forgetting the password set during installation and getting locked out of the default admin account — most installers let you reset it, but it is worth writing it down immediately during setup.
- Practicing directly inside a database the tool itself uses internally (like "postgres" or "mysql") instead of creating a separate practice database, risking confusing errors if something is accidentally dropped or altered.
- Mixing up the default ports (5432 for PostgreSQL vs 3306 for MySQL) when configuring a GUI client connection, resulting in a generic "connection refused" error.
- Assuming a hosted free-tier database has no limits — most free tiers cap storage, connection count, or automatically pause an idle database after a period of inactivity.
Exercise
Create a table named students with an integer id as primary key and a text name. Insert two students, Asha and Ravi. Then count the rows, naming the result column total.
Expected output: total then 2
-- TODO: create the students table
-- TODO: insert Asha (id 1) and Ravi (id 2)
-- TODO: count the rows as "total"
Show hints
- A column is declared as name, type and constraints:
id INTEGER PRIMARY KEY. - A result column is named with
AS:SELECT COUNT(*) AS total FROM students.
Show solution
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT
);
INSERT INTO students (id, name) VALUES (1, 'Asha'), (2, 'Ravi');
SELECT COUNT(*) AS total FROM students;
Quick quiz
1. What is psql?
- A. A graphical database designer
- B. The command-line client for PostgreSQL
- C. A programming language
- D. A backup format
Show answer
B. It connects to a PostgreSQL server and runs SQL typed at its prompt.
2. Which statement creates a new, empty database?
- A. NEW DATABASE shop;
- B. MAKE DATABASE shop;
- C. CREATE DATABASE shop;
- D. ADD DATABASE shop;
Show answer
C. Tables are then created inside it with CREATE TABLE.
3. Why practise on a local database instead of a shared one?
- A. You can experiment and make mistakes without affecting anyone else's data
- B. Local databases are faster for production
- C. SQL only works locally
- D. Shared databases do not support SELECT
Show answer
A. A practice database can be dropped and recreated at any time.
Interview questions
What is the difference between a database server and a database client?
Show answer
The server is the program that stores the data, enforces the rules and executes queries; PostgreSQL and MySQL are servers. A client is any program that connects to it and sends SQL: a command-line tool such as psql, a graphical tool such as DBeaver or pgAdmin, or your application through a driver. Many clients can be connected to one server at the same time.
What is the difference between a database, a schema and a table?
Show answer
A table holds rows of one kind of data, such as customers. A schema is a named group of tables and other objects inside a database; PostgreSQL puts tables in the public schema by default. A database is the top-level container that holds schemas and is what a client connects to. In MySQL the words schema and database mean the same thing.
Key Points to Remember
- PostgreSQL and MySQL are both solid, free choices for learning SQL locally, with nearly identical core syntax.
- GUI clients (TablePlus, DBeaver, pgAdmin) and CLI tools (psql, mysql) are two equally valid ways to connect and run queries.
- Hosted free-tier services (Supabase, Neon, PlanetScale) are a no-install alternative that behaves like a real remote database.
- Creating a dedicated practice database, separate from any system database, keeps experimentation safe.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs your SQL on SQLite in your browser. Standard SQL works; a few features that exist only in PostgreSQL or MySQL do not. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
These examples need a project environment on your computer, so they can be copied but not run here. Codelab runs Python, FastAPI backends with SQLite, plain Java 17 and HTML/CSS/JavaScript.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering