SQL for Beginners - A Complete Guide.

Introduction Structured Query Language (SQL) is a language that is used to interact with SQL databases. In today's data-driven world, data professionals e.g. data analysts, data engineers, data scientists need to retrieve data from databases, and SQL is the tool used for that job. This post will take you through the basics of SQL which include command categories, commands and resources where you can learn SQL for free. SQL Command Categories. SQL commands are divided into four major categories which include: i. Data Manipulation Language(DML) These commands are used to manipulate data in databases. These commands include: SELECT INSERT DELETE UPDATE ii. Data Definition Language (DDL) These commands are used to define or change the structure of database objects like tables and schemas. These commands include: CREATE ALTER DROP iii. Data Control Language (DCL) These commands are used to control access to data in the database. They include: GRANT ROLLBACK COMMIT Basic SQL Commands These are commands that you'll use in your day-to-day basic while interacting with databases using SQL. 1. CREATE This command is used to create a database, schema or tables in SQL. CREATE DATABASE companydb; CREATE TABLE users ( id SERIAL PRIMARY KEY, username VARCHAR(50), email VARCHAR(50), isAdmin BOOLEAN DEFAULT false, ) The first query creates a database called companydb The second query creates a table called users with the following columns: id which will be the primary identifier for our rows in the table. The SERIAL is a data type shortcut used when you want a column to automatically generate a unique number for each new row. username is the user's username which should not exceed 50 characters. email is the user's email. isAdmin is a boolean value which shows if a user is an administrator. If this value is not filled manually, the database will provide a default value false. 2. SELECT This command is used to fetch columns and rows from a database. SELECT username, email FROM users; This gets all rows under the username and email columns in the users table. 3. WHERE This command fetches data based on given conditions. SELECT username, email FROM users WHERE isAdmin = false; This gets all the rows from the username and email columns in the users table who are not administrators. 4. INSERT INTO This command inserts data into a database. INSERT INTO users (username, email, isAdmin) VALUES ('johndoe', 'john@domain.com', true); This inserts the following data into the database. NOTE: The values in the brackets should match the columns in the database. You can also add many rows into the database as so: INSERT INTO users (username, email, isAdmin) VALUES ('johndoe', 'john@domain.com', true) ('janeburrows', 'jane@domain.com', false) ('danielsparrow', 'sdaniels@domain.com', true); 5. UPDATE This command is used to modify existing data in the database UPDATE users SET isAdmin = false WHERE username = 'johndoe'; This updates the isAdmin value for johndoe to false. 6. DELETE This command removes data from a database DELETE FROM users WHERE username = 'johndoe'; NOTE: Be very careful when running the DELETE command. Only run this command when needed to do so to avoid losing important information! 7. ORDER BY This command is used to sort out results in ascending or descending order. SELECT username FROM users ORDER BY username ASC This sorts the data in ascending order based on the username column. SELECT username FROM users ORDER BY username DESC This sorts the data in descending order based on the username column. Other commands include JOIN, GROUP BY, HAVING among others which are quite advanced and you'll learn in your journey to becoming a SQL wizard. Uses of SQL. SQL is used for various reasons which include: a. Analyzing customer and business data. b. Creating power dashboards and reports. c. Extracting data for machine learning. d. Cleaning and transforming datasets. e. Collaborating with backend developers and data engineers. Online Resources to learn SQL There are very many online resources where one can learn SQL, both free and paid courses. Some of these resources that I have personally used and have worked wonders for me include SQLBolt - Great for learning SQL using problem solving approach by answering various quizzes after each topic Bro Codes 3 hour SQL tutorial - A good Youtube video with hands-on explanation on different aspects of SQL. Codecademy - It's great for beginners but require you to pay a fee to obtain a completion certificate. Conclusion SQL is the starting point for anyone working with data. The more you practice writing queries, the more natural it becomes. Whether you're working on data pipelines, reports,

Apr 14, 2025 - 11:35
 0
SQL for Beginners - A Complete Guide.

Introduction

Image description

Structured Query Language (SQL) is a language that is used to interact with SQL databases. In today's data-driven world, data professionals e.g. data analysts, data engineers, data scientists need to retrieve data from databases, and SQL is the tool used for that job.

This post will take you through the basics of SQL which include command categories, commands and resources where you can learn SQL for free.

SQL Command Categories.

SQL commands are divided into four major categories which include:

i. Data Manipulation Language(DML)

These commands are used to manipulate data in databases. These commands include:

  • SELECT
  • INSERT
  • DELETE
  • UPDATE

ii. Data Definition Language (DDL)

These commands are used to define or change the structure of database objects like tables and schemas. These commands include:

  • CREATE
  • ALTER
  • DROP

iii. Data Control Language (DCL)

These commands are used to control access to data in the database. They include:

  • GRANT
  • ROLLBACK
  • COMMIT

Basic SQL Commands

These are commands that you'll use in your day-to-day basic while interacting with databases using SQL.

1. CREATE

This command is used to create a database, schema or tables in SQL.

CREATE DATABASE companydb;

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50),
    email VARCHAR(50),
    isAdmin BOOLEAN DEFAULT false,
)

The first query creates a database called companydb
The second query creates a table called users with the following columns:

  • id which will be the primary identifier for our rows in the table. The SERIAL is a data type shortcut used when you want a column to automatically generate a unique number for each new row.
  • username is the user's username which should not exceed 50 characters.
  • email is the user's email.
  • isAdmin is a boolean value which shows if a user is an administrator. If this value is not filled manually, the database will provide a default value false.

2. SELECT

This command is used to fetch columns and rows from a database.

SELECT username, email FROM users;

This gets all rows under the username and email columns in the users table.

3. WHERE

This command fetches data based on given conditions.

SELECT username, email FROM users
WHERE isAdmin = false;

This gets all the rows from the username and email columns in the users table who are not administrators.

4. INSERT INTO

This command inserts data into a database.

INSERT INTO users (username, email, isAdmin)
VALUES ('johndoe', 'john@domain.com', true);

This inserts the following data into the database.

NOTE: The values in the brackets should match the columns in the database. You can also add many rows into the database as so:

INSERT INTO users (username, email, isAdmin)
VALUES ('johndoe', 'john@domain.com', true)
       ('janeburrows', 'jane@domain.com', false)
       ('danielsparrow', 'sdaniels@domain.com', true);

5. UPDATE

This command is used to modify existing data in the database

UPDATE users
SET isAdmin = false
WHERE username = 'johndoe';

This updates the isAdmin value for johndoe to false.

6. DELETE

This command removes data from a database

DELETE FROM users
WHERE username = 'johndoe';

NOTE: Be very careful when running the DELETE command. Only run this command when needed to do so to avoid losing important information!

7. ORDER BY

This command is used to sort out results in ascending or descending order.

SELECT username FROM users
ORDER BY username ASC

This sorts the data in ascending order based on the username column.

SELECT username FROM users
ORDER BY username DESC

This sorts the data in descending order based on the username column.

Other commands include JOIN, GROUP BY, HAVING among others which are quite advanced and you'll learn in your journey to becoming a SQL wizard.

Uses of SQL.

SQL is used for various reasons which include:

a. Analyzing customer and business data.

b. Creating power dashboards and reports.

c. Extracting data for machine learning.

d. Cleaning and transforming datasets.

e. Collaborating with backend developers and data engineers.

Online Resources to learn SQL

There are very many online resources where one can learn SQL, both free and paid courses.

Some of these resources that I have personally used and have worked wonders for me include

  • SQLBolt - Great for learning SQL using problem solving approach by answering various quizzes after each topic
  • Bro Codes 3 hour SQL tutorial - A good Youtube video with hands-on explanation on different aspects of SQL.
  • Codecademy - It's great for beginners but require you to pay a fee to obtain a completion certificate.

Conclusion

SQL is the starting point for anyone working with data. The more you practice writing queries, the more natural it becomes. Whether you're working on data pipelines, reports, or APIs, knowing SQL will save you time and open up opportunities in every tech field.

If you found this post helpful, please leave a ❤️, comment, and share to help others get started with SQL too!