SQL Tutorial for Beginners

post

you will learn about the basics of SQL, its history, how it works, its uses, and much more. Let’s begin!

What is SQL?

SQL stands for Structured Query Language. It is a programming or query language used to perform operations on relational databases. These operations include creating, managing, updating, or retrieving data from a database.

The core functions in any database are referred to as CRUD operations—Create, Read, Update, and Delete. Databases are crucial in today’s digital age where large volumes of data are generated every day.

SQL is an essential tool for anyone who works with data regularly. It allows users to insert, retrieve, update, and delete data stored in databases efficiently.

History of SQL

SQL was originally developed by IBM researchers Raymond F. Boyce and Donald D. Chamberlin in the 1970s. The early version was named SEQUEL, short for Structured English Query Language. It was designed to manipulate and retrieve data from IBM databases.

IBM later released commercial versions such as System/38, SQL/DS, and DB2 in 1979, 1981, and 1983 respectively. In 1986, SQL became a standard when ANSI and ISO adopted it as the official database language.

How SQL Works

SQL operates through commands and queries directed at a relational database. In a relational database, data is stored and managed in the form of tables or relations.

When a user runs a command in a tool like SQL Workbench, the system processes the command and returns results in a table format.

Next, we’ll look into SQL commands and their types.

What Can SQL Do?

SQL allows you to:

Run queries on databases.

Perform CRUD operations.

Create and manage databases.

Update and manipulate existing records.

Create views based on existing tables.

Set permissions for different database users.

Execute transactions and work seamlessly with programming languages like C++, Java, and Python.

Rules for Writing SQL Queries

When writing SQL queries, follow these rules:

SQL is not case-sensitive, but keywords are usually written in UPPERCASE for clarity.

Queries can span multiple lines.

SQL is based on relational algebra and tuple relational calculus.

SQL can carry out nearly all data-related tasks in a database.

SQL Processes

When a query is executed in a DBMS or RDBMS, the system looks for the most efficient execution path.

The execution process involves components like the query dispatcher, engine, and optimizer.

SQL queries do not deal with logical files; traditional query engines handle non-SQL operations.

Uses of SQL

SQL is integrated into many technologies and is vital in fields like data science, analytics, machine learning, AI, web development, big data, and blockchain.

Some common uses include:

Database Transaction Management – Managing and configuring DBMS systems.

Reporting – Extracting insights from large datasets.

Manual Analysis – Identifying and correcting issues in stored data.

Business Applications – Built-in functions simplify heavy data processing tasks.

SQL Commands and Their Types

SQL commands are grouped based on their purpose:

1. DDL (Data Definition Language)

DDL commands define the structure or schema of a database.

CREATE – To create database objects.

ALTER – To modify structure.

DROP – To delete database objects.

RENAME – To rename objects.

TRUNCATE – To delete all rows from a table.

Example: Create Table

CREATE TABLE Debugshala_Employee (  name_emp  VARCHAR(50),  post_emp  VARCHAR(50),  email     VARCHAR(50),  age       INT,  salary    VARCHAR(10) );

This command creates a table named Debugshala_Employee with five columns: name, post, email, age, and salary.

2. DML (Data Manipulation Language)

Used to handle data within tables.

a. INSERT

Used to add new records to a table.

INSERT INTO Debugshala_Employee (name_emp, post_emp, email, age, salary) VALUES  ('Ram', 'Intern', 'ram@debugshala.com', 21, '10000'),  ('Shyam', 'Manager', 'shyam@debugshala.com', 25, '25000'),  ('Ria', 'Analyst', 'ria@debugshala.com', 23, '20000'),  ('Kavya', 'Senior Analyst', 'kavya@debugshala.com', 31, '30000'),  ('Aman', 'Database Operator', 'rish@debugshala.com', 26, '15000');

b. UPDATE

Used to modify existing data.

UPDATE Debugshala_Employee SET salary = 12000, age = 26 WHERE name_emp = 'Ria';

c. DELETE

Used to remove records.

DELETE FROM Debugshala_Employee WHERE name_emp = 'Kavya';

3. DQL (Data Query Language)

Used to fetch data from the database.

SELECT

-- View all data SELECT * FROM Debugshala_Employee; -- View first two rows SELECT * FROM Debugshala_Employee LIMIT 2; -- View specific columns SELECT name_emp, age FROM Debugshala_Employee;

4. TCL (Transaction Control Language)

Manages changes made by DML statements.

COMMIT – Saves all changes permanently.

ROLLBACK – Reverts changes to the last committed state.

SAVEPOINT – Sets a point to roll back to within a transaction.

5. DCL (Data Control Language)

Used to manage user permissions.

GRANT – Gives users access rights.

REVOKE – Withdraws access rights.

Note: In some databases like Oracle, DCL commands result in implicit commits, so rollback might not work.

Summary

In this tutorial, we explored the basics of SQL: what it is, how it works, its key uses, and major command types like DDL, DML, DQL, TCL, and DCL. With this foundation, you're ready to dive deeper into working with databases using SQL.


Share This Job:

Write A Comment

    No Comments