What is SQL? (A beginner’s guide to SQL)

“May the Force be with you” is a line every Star Wars fan knows, but here in the tech world, we might say, “May the Data be with you!” Since the 1970s, SQL (structured query language) has been the Jedi Master of database management, shaping the way we handle and analyze data.

Today, over 90% of Fortune 500 companies utilize SQL to harness the power of their data, making it an indispensable tool in the digital age. SQL’s ability to manage and manipulate vast amounts of data efficiently has revolutionized industries ranging from finance to healthcare, enabling businesses to make data-driven decisions with unprecedented accuracy.

But what exactly is SQL and databases? To better understand the functionality and importance of SQL, let’s liken it to a library system—a place we all can relate to.

For a more visual experience, we’ve embedded a video to complement the information in this post. You can check it out here:

Imagine a grand library where the organization of books and the efficiency of the librarian mirror the structure and operations of SQL in managing data.

Organized rows of books in a serene library setting

The Library (Database)

In our analogy, the library itself represents a database, just as a library houses a collection of books, a database stores and organizes data tables. Picture walking into a vast library, filled with rows upon rows of bookshelves. Each bookshelf is labeled and arranged meticulously to ensure everything is in its rightful place. Similarly, in a database, tables are the fundamental units that hold and organize data. A unique name defines each table and contains rows and columns that store records and attributes. The database, like the library, is designed to store information systematically so that it can be easily accessed and managed.

Library Organizational System (RDBMS)

Now, let’s consider the library’s organizational system, which is crucial for maintaining order and accessibility. This system ensures that books are categorized, labeled, and shelved correctly. In the digital world, this is akin to the Relational Database Management System (RDBMS). The RDBMS is the backbone of how data is stored, retrieved, and managed in a structured and efficient manner. It enforces rules, relationships, and constraints to keep the data organized, just as the library’s organizational system ensures that every book has a specific place. The RDBMS manages the interactions between the user and the database, ensuring data integrity, security, and performance. It handles tasks such as indexing, querying, and transaction processing, much like how a library’s organizational system ensures books are correctly cataloged, shelved, and accessible.

Image source: Relational Database Management System – RDBMS – Nexnet Solutions

Book Shelves (Tables)

Each bookshelf in the library corresponds to a table in the database. On these shelves, each book symbolizes a record in a database table. This record could be a customer record, a product description, or any other piece of information that the database needs to manage. Just as books are categorized and placed on specific shelves, records in a database table are systematically organized. Each table consists of columns (attributes) and rows (records), where each row represents a single entry or item of data. For instance, in a library, you might have a bookshelf dedicated to fiction books, with each book representing a different novel. In a database, you might have a table dedicated to customer information, with each row representing a different customer’s data.

Library Catalogue (Index)

Navigating a large library would be nearly impossible without a library catalog. This catalog acts as an index, providing quick references to the location of each book. Similarly, in SQL, indexes help expedite data retrieval, allowing for faster access to specific records. Without indexes, finding the necessary information in a massive database would be a time-consuming task. Indexes in a database are special lookup tables that the database search engine can use to speed up data retrieval. They function much like a library’s card catalog, pointing to the location of the records without having to search the entire database. Indexes are created on columns that are frequently used in search conditions or queries, thereby enhancing the performance and efficiency of data retrieval operations.

Librarian (SQL Query)

The librarian is the expert who efficiently locates and manages the library’s vast collection of books. In databases, the SQL query serves this role. SQL, as the query language, possesses the expertise to interact with databases seamlessly. It allows users to retrieve, update, insert, and delete data with precision. The SQL query is the command given to the database to fetch or manipulate the data as needed, this is like asking the librarian to find a specific book. For example, if you want to find all books by a particular author, you would ask the librarian, who knows exactly where to look and how to retrieve those books quickly. Similarly, if you want to find all customer records with a specific attribute, you would use an SQL query to search the database efficiently.

What Can You Do with SQL?

SQL is a powerful language with a wide range of capabilities. Here are some of the essential functions you can perform with SQL:

  • Execute Queries: The most common use of SQL is to execute queries against a database to retrieve data. This allows users to select specific information from tables, such as finding all customers in a particular city.
  • Create and Modify Tables: SQL provides commands to create and modify new tables. This includes defining the structure of the table, adding new columns, or changing the data types of existing columns.
  • Insert Data: SQL allows you to insert new records into tables. You can add data row by row or in bulk, depending on your needs.
  • Update Data: With SQL, you can update existing records in a table. This is useful for modifying data that needs to be corrected or updated with new information.
  • Delete Data: SQL enables you to delete records from a table. You can remove specific rows based on a condition or clear all data from a table.
  • Join Tables: SQL’s JOIN operations allow you to combine data from multiple tables based on related columns. This is essential for complex queries needing information from multiple tables.
  • Create Indexes: SQL lets you create indexes on tables to improve the speed of data retrieval operations. Indexes help the database quickly locate the desired data.
  • Manage Transactions: SQL supports transaction control, allowing you to ensure data integrity by grouping multiple operations into a single, atomic transaction.

SQL Command Types

SQL commands are categorized into several types, each serving a specific purpose in database management. Here are the primary SQL command types:

DDL (Data Definition Language): DDL commands define and manage the structure of the database objects. Common DDL commands include:

COMMANDDESCRIPTIONSYNTAX
CREATECreate a Database or its objects (tables, index, views, triggers, functions, and stored procedures)CREATE DATABASE database_name;

CREATE TABLE table_name (column 1 data_type, column 2 data_type);
ALTERAlter the structure of a databaseALTER TABLE table_name;
DROPDelete objects from the databaseDROP TABLE table_name;
TRUNCATERemove all records from a table, including all spaces allocated for the recordTRUNCATE TABLE table_name;
RENAMERename an object existing in the databaseRENAME TABLE old_table_name TO new_table_name;

DML (Data Manipulation Language): DML commands manipulate the data within database objects. Common DML commands include:

COMMANDDESCRIPTIONSYNTAX
INSERTInsert data into a tableINSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);
UPDATEUpdate existing records within the tableUPDATE table_name
SET column1 = value1, column2 = value2
WHERE condition;
DELETEDeletes records from a databaseDELETE FROM table_name WHERE condition;

DQL (Data Query Language): DQL commands control access to data within the database. Common DQL commands include:

COMMANDDESCRIPTIONSYNTAX
SELECTRetrieves data within the table or databaseSELECT * FROM table_name

SELECT column_name FROM table_name

DCL (Data Control Language): With the Data Control Language, you can carry out grant and revoke actions within the database. And remember what we said about permission? You do a lot of things within tables in a database. You could say that the command is used to carry out administrative tasks within the database itself. DCL commands include:

COMMANDDESCRIPTIONSYNTAX
GRANTAssigns new privileges to a user account, allowing access to specific database objects, actions, or functionsGRANT privilege_type [(column_list)]
ON [object_type] object_name
TO user [WITH GRANT OPTION];
REVOKERemoves previously granted privileges from a user account, taking away their access to certain database objects or actions.REVOKE [GRANT OPTION FOR] privilege_type [(column_list)]
ON [object_type] object_name FROM user [CASCADE];

TCL (Transaction Control Language): TCL commands are used to manage transactions within the database. Common TCL commands include:

COMMANDDESCRIPTIONSYNTAX
BEGINTRANSACTIONStarts a new transactionBEGIN TRANSACTION [transaction_name];
COMMITSaves all changes made during the transactionCOMMIT;
ROLLBACKUndoes all changes made during the transactionROLLBACK;
SAVEPOINTCreates a savepoint within the current transactionSAVEPOINT savepoint_name;

Now that you know about SQL, do you want to learn how to write your first code in SQL? You can check out the links here to learn more about SQL – W3 Schools, GeeksforGeeks.

Featured image: People illustrations by Storyset

David Ezekiel
David Ezekiel

Hi. I am David Ezekiel.

I am a Data Analyst passionate about unraveling the stories hidden within data and empowering others to harness its transformative power. From uncovering actionable insights to driving strategic decision-making, my core passion lies in leveraging data to unlock new possibilities and drive real-world impact.

Articles: 11

Leave a Reply

Your email address will not be published. Required fields are marked *