Sitemap
Level Up Coding

Coding tutorials and news. The developer homepage gitconnected.com && skilled.dev && levelup.dev

SQL Tutorial for Beginners

7 min readSep 11, 2026

--

Press enter or click to view image in full size

What is an SQL database?

An SQL database is a type of relational database management system (RDBMS) that uses Structured Query Language (SQL) as its interface for interacting with and manipulating data.

Press enter or click to view image in full size

Press enter or click to view image in full size

In a SQL database, data is stored in tables like spreadsheets with rows and columns. Each row represents a single record, and each column represents a field within the record.

Where are SQL databases stored?

SQL databases are stored on data storage systems within a separate server, typically on hard drives or solid-state drives.

Examples:

Popular SQL databases are MySQL, PostgreSQL, Oracle Database, and Microsoft SQL Server. They are widely used in applications that require complex transactions and efficient querying, especially in traditional enterprise apps.

Press enter or click to view image in full size

Press enter or click to view image in full size

SQL Data Structure in Tables

Tables

Tables are the fundamental building blocks of SQL databases, similar to spreadsheets. Each table holds data about a specific topic, such as customers, orders, or products.

Columns

Each table consists of columns, which can be thought of as the “fields” or “attributes” of the table. For example, a “Customers” table might have columns for customer_id, name, age, and email.

Data Types

Each column in a SQL table is required to have a specified data type. Common data types include INTEGER, VARCHAR (variable character string), BOOLEAN, FLOAT, DATE, etc.

Rows

Each row in a table represents a single record. In the “Customers” table, a row would represent all the data for one customer, with the actual values for each column (e.g., customer ID 123, name “John Doe”, etc.).

Press enter or click to view image in full size

Press enter or click to view image in full size

Relationships and Keys

Relationships

Relationships in SQL databases define how tables relate to each other. The most common types are:

  • One-to-One: Each row in Table A is linked to one and only one row in Table B.
  • One-to-Many (or Many-to-One): A single row in one table can relate to many rows in another table.
  • Many-to-Many: Rows in Table A can relate to multiple rows in Table B and vice versa.

Keys

Keys help us to set up these relationships between tables. The two most commonly used keys are:

  • Primary Key: A column (or a set of columns) used to uniquely identify each row in a table.
  • Foreign Key: A column (or a set of columns) in one table that uniquely identifies a row of another table. It creates a link between the two tables.

Press enter or click to view image in full size

Press enter or click to view image in full size

Joining Tables

SQL uses the JOIN clause to combine rows from two or more tables based on a related column between them.

Common types of joins include INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.

SQL Query Examples to Join 2 Tables

Assuming we have two tables, Orders (with columns order_id, customer_id, and order_date) and Customers (with columns customer_id, name, and address), here's how you might join them:

Get Hayk Simonyan’s stories in your inbox

Join Medium for free to get updates from this writer.

INNER JOIN (returns records with matching values in both tables)

SELECT Orders.id, Customers.name 
FROM Orders
INNER JOIN Customers
ON Orders.customer_id = Customers.customer_id;

LEFT JOIN (returns all records from the left table and matched records from the right table)

SELECT Orders.id, Customers.name 
FROM Orders
LEFT JOIN Customers
ON Orders.customer_id = Customers.customer_id;

Press enter or click to view image in full size

Press enter or click to view image in full size

In these examples, the INNER JOIN fetches only the orders with corresponding customer information, while the LEFT JOIN fetches all orders, including those without corresponding customer information (the missing values are filled with NULLs). The specific type of join used depends on the exact nature of the data and the desired results.

SQL Schema

A schema is what defines how the entire database is organized in these tables. It includes tables, the relationships between them, and the structure of each table (columns and data types).

It acts as a blueprint for storing data in the database and ensures that data is organized consistently.

Indexes

The final topic I’d like to discuss is SQL Indexes. Indexes in SQL databases are special lookup tables that the database search engine can use to speed up data retrieval.

Suppose our example Customers table has thousands of records. We often need to query customers based on their city, which is not the primary key of the table. In this scenario, an index on the city column can significantly speed up these queries.

Without Index

If there’s no index on the city column, a query to find customers in a specific city, say "New York," would require a full table scan. This is inefficient, especially for large tables.

SELECT name, email
FROM Customers
WHERE city = 'New York';

With Index

To optimize this query, we create an index on the city column.

CREATE INDEX idx_city
ON Customers (city);

Now, when the same query is executed, the database uses the index idx_city to quickly find all customers in "New York" without scanning the entire table.

SELECT name, email
FROM Customers
WHERE city = 'New York';

Press enter or click to view image in full size

Press enter or click to view image in full size

The difference in performance is most noticeable with large data sets. For small tables, the impact might be negligible or even negative due to the overhead of maintaining the index.

When Do We Use Indexes?

  1. Improving Query Performance: The primary reason for using indexes is to speed up query performance, especially for large tables. Without an index, the database has to perform a full table scan, which is a slow operation for large tables.
  2. Unique Constraints: Indexes can enforce uniqueness for columns when you want to ensure that no two rows have the same value in certain columns.
  3. Sorting and Grouping Speed: Indexes improve the speed of data retrieval operations by efficiently locating and returning the data. They are particularly beneficial for queries involving JOINs, ORDER BY, and GROUP BY clauses.

Downsides of Indexes

While indexes are critical for improving query performance, they come with trade-offs:

  1. Increased Storage: Each index you create consumes additional disk space. The amount of space required depends on the size of the table and the number and type of columns used in the index.
  2. Maintenance Overhead: Indexes must be updated whenever data is inserted, deleted, or updated in the table. This means that write operations (INSERT, UPDATE, DELETE) can be slower on tables with many indexes.
  3. Complexity in Optimization: Having too many indexes can make the query optimizer’s job harder, potentially leading to suboptimal query plans.

Press enter or click to view image in full size

Press enter or click to view image in full size

What Data Structures are used in SQL

SQL databases typically use several data structures to store, index, and manage data. Here are the most common data structures used in SQL databases.

Indexes are stored separately from the table data, usually in a structure that is optimized for quick search and retrieval.

  1. Most indexes are stored as B-Trees or variations (like B+ Trees). B-Trees are the most common data structure used for indexing in SQL databases. They allow for fast lookups, insertions, and deletions. B-Trees keep data sorted and allow searches, sequential access, insertions, and deletions in logarithmic time. B+ Trees are a variant of B-Trees often used in databases and filesystems. They store all actual data in leaf nodes, and the internal nodes only contain keys to the leaf nodes. This structure makes them particularly efficient for range queries and full table scans.
  2. Hash Indexes: Hash indexes use a hash table with a hash function to map keys to specific positions in an index. They are very efficient for point queries where the exact match is known. However, they are inefficient for range queries and do not store data in a sorted order.
  3. Heap (Unsorted Structure): A database might use a heap structure in some simple cases, particularly for small tables or temporary workspaces. This means that the data is unsorted and inserted wherever there’s space. Heaps are fast for insertion but can be inefficient for queries as they might require a full table scan.
  4. Trees and Binary Trees: For some specialized use cases, databases might use other types of trees, including binary trees, AVL trees, or red-black trees, each offering different performance characteristics for balancing, searching, inserting, and deleting data.
  5. R-Trees: R-Trees are a type of data structure used for spatial access methods, i.e., for indexing multi-dimensional information such as geographical coordinates. They are commonly used in geographic information systems (GIS) and databases that deal with spatial data.
  6. Tries: Tries (prefix trees) are occasionally used for special cases like indexing certain string data types. They can provide an efficient means of searching for keys with common prefixes.

--

--

Written by Hayk Simonyan

I help software engineers break out of the mid-level trap and scale into multi six-figure remote careers: https://youtube.com/@hayk.simonyan