SQL-Part1
SQL-Part1
1. INTRODUCTION
SQL, which stands for Structured Query Language, is a programming language used
for managing and manipulating relational databases. It was created in the 1970s by
IBM and became a de facto standard for data management in many applications and
systems.
The history of SQL dates back to the 1970s when researchers at IBM, notably Donald
D. Chamberlin and Raymond F. Boyce, began working on a query language for
relational databases. This work led to the development of SEQUEL (Structured
English Query Language) in 1974, which was later renamed SQL due to trademark
issues.
Data Definition Language (DDL): Used to create and delete objects in the database
(tables, integrity constraints, views, etc.).
Example commands: CREATE, DROP, ALTER
Data Control Language (DCL): Used to manage access rights on database objects
(creating users and assigning their rights).
Example commands: GRANT, REVOKE
Data Manipulation Language (DML): Used to query, insert, update, and delete data.
DML is based on relational operators, with added functions for aggregate calculations
and commands for performing insert, update, and delete operations.
Example commands: INSERT, UPDATE, DELETE, SELECT
Transaction Control Language (TCL): Used for transaction management
(committing or rolling back changes to data in the database).
Example commands: COMMIT, ROLLBACK
In this section, we focus on the Data Manipulation Language (DML) of SQL, which is
indeed the most widely used part in practice.
SELECT column_name
FROM table_name;
Example:
Imagine a database called "Student" that contains information about students at a university.
Table "Student":
If we want to get a list of all the cities of the students, we simply perform the following query:
SELECT City
FROM Student;
City
Alger
Bouira
Msila
Msila
Setif
It is possible to read multiple columns at once by simply writing the names of the
desired columns after SELECT and separating them with commas. For example, to get
the names and last names of the students, we should execute the following query:
SELECT FirstName, LastName
FROM Student;
It is also possible to return all columns from a table without listing their names by using the ""
character (asterisk). The "" character in a SELECT clause in SQL is a shortcut that means "all
columns."
Example:
SELECT * FROM Student;
FROM table_name;
Example:
In the previous example, displaying the cities of the students gives duplicate city names. To
avoid this redundancy, we simply add the DISTINCT keyword after SELECT as follows:
SELECT DISTINCT City
FROM Student;
City
Alger
Bouira
Msila
Setif
Example:
To display only the students from the city of M’sila, write:
SELECT *
FROM Student
WHERE City = 'Msila';
Comparison operators
In the SQL WHERE command, various comparison operators can be used to specify
conditions that must be met by the data. These operators allow you to filter rows based on
specific criteria. Here are the main comparison operators that can be used with WHERE:
Equality Comparison:
=: Equal to
!= or <>: Not equal to
Numeric Comparison:
Text Comparison:
LIKE: Matches a pattern (typically used with wildcard characters like % to represent
multiple characters or _ for a single character)
NOT LIKE: Does not match a pattern
Range Comparison:
List Comparison:
In SQL, the logical operators AND and OR are used to combine multiple conditions in a
WHERE clause. They allow specifying more complex conditions to filter data from a table.
SELECT column_names
FROM table_name
WHERE condition1 AND condition2;
SELECT column_names
FROM table_name
WHERE condition1 OR condition2;
These operators can be combined endlessly and mixed. The example below filters the results
of the table table_name if either condition1 and condition2 or condition3 is true:
SELECT column_names
FROM table_name
WHERE condition1 AND (condition2 OR condition3);
Note: It is important to use parentheses when needed to avoid errors and to improve query
readability.
Example:
SELECT *
FROM produit
WHERE Categorie = 'Meuble' AND Quantite < 20;
SELECT *
FROM produit
WHERE Categorie = 'Informatique' OR Quantite >= 20;
2.6. SQL IN
In SQL, the IN command is used to specify a condition where the value of a column must
match one of the values provided in a list, following the syntax:
SELECT column_name
FROM table
WHERE column_name IN (value1, value2, value3, ...);
Example :
SELECT *
FROM Student
WHERE City_St IN ('Alger', 'Msila');
Notes:
To negate the IN command in SQL, we can use NOT IN. This allows us to select
records that do not match any value in the specified list.
SELECT *
FROM Student
The syntax used with the IN operator is simpler than using multiple OR operators. To
illustrate this clearly, here are two queries that return the same result — one using IN, and
the other using multiple OR conditions:
SELECT *
FROM Student
------------------------
SELECT *
FROM Student