0% found this document useful (0 votes)
3 views

SQL-Part1

SQL (Structured Query Language) is a programming language developed in the 1970s for managing relational databases, becoming a standard for data management. It consists of four sublanguages: DDL, DCL, DML, and TCL, with DML being the most widely used for querying and manipulating data. Key SQL commands include SELECT, DISTINCT, AS, WHERE, AND, OR, and IN, which allow users to retrieve and filter data effectively.

Uploaded by

anis.bobi000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SQL-Part1

SQL (Structured Query Language) is a programming language developed in the 1970s for managing relational databases, becoming a standard for data management. It consists of four sublanguages: DDL, DCL, DML, and TCL, with DML being the most widely used for querying and manipulating data. Key SQL commands include SELECT, DISTINCT, AS, WHERE, AND, OR, and IN, which allow users to retrieve and filter data effectively.

Uploaded by

anis.bobi000
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SQL (Structured Query Language)

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.

 1974: Donald D. Chamberlin and Raymond F. Boyce at IBM developed SEQUEL


(Structured English Query Language), a query language for relational databases.
 1979: Oracle Corporation (then known as Software Development Laboratories)
developed its own version of SQL for its Oracle database management system.
 1986: The American National Standards Institute (ANSI) adopted SQL as the official
standard for relational databases, and this standardized version is often referred to as
SQL-86.
 1989: The International Organization for Standardization (ISO) also adopted SQL as
an international standard, known as SQL-89 or SQL1.
 1992: SQL-92, also known as SQL2, became the first major revision of the SQL
standard, introducing new features such as triggers, views, distributed transactions,
etc.
 1999: SQL-99, also known as SQL3, was a major update to the SQL standard,
introducing features like regular expressions, stored procedures, and the addition of
data types such as geometric types and XML types.
Since then, several revisions of the SQL standard have been released, each adding new
features and improving the capabilities of the language. SQL implementations have
proliferated with the development of different database management systems
(DBMS), with the most popular being MySQL, PostgreSQL, Oracle Database,
Microsoft SQL Server, SQLite, and others.
Today, SQL remains the standard language for interacting with relational databases,
and its importance in data management continues to grow with the rise of web,
mobile, and enterprise applications.

2. CONSTITUENTS OF SQL LANGUAGE


SQL is a declarative language, meaning it is not strictly a programming language but
rather a standard interface for accessing databases. It consists of four sublanguages:

 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.

2.1 SQL SELECT


The most common use of SQL is to read data from a database. This is done using the
SELECT command, which returns records in a result set. This clause allows users to specify
the specific columns they wish to retrieve in a query, as well as criteria to filter the data.
The basic syntax for the SELECT clause is as follows:

SELECT column_name
FROM table_name;

Example:
Imagine a database called "Student" that contains information about students at a university.
Table "Student":

ID FirstName LastName BirthDate City


1 Nadir Benslimane 21/03/2012 Alger
2 Lyes Hammam 05/05/2010 Bouira
3 Abir Saoucha 13/08/2011 Msila
4 Sofiane Bakhti 02/01/2010 Msila
5 Hind Boubakri 25/11/2012 Setif

If we want to get a list of all the cities of the students, we simply perform the following query:

SELECT City
FROM Student;

This will return the following result:

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;

2.2 SQL DISTINCT


In SQL, using the SELECT command allows you to retrieve data from one or more columns
in a table. However, this command may potentially display duplicate rows, making the results
less readable. To avoid these redundancies, we can simply add the DISTINCT keyword after
SELECT. This eliminates duplicate rows from the result and only presents unique values. The
basic syntax to use DISTINCT is as follows:
SELECT DISTINCT column1, column2, ...

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;

This will give the following result:

City
Alger
Bouira
Msila
Setif

2.3 SQL AS (Alias)


In SQL, an alias is an alternative name that we can assign to a table or column in a query to
make the results more readable or to shorten the code. Aliases are often used to simplify long
or complex names and temporarily rename columns or tables.
SELECT FirstName AS FN, LastName AS LN
FROM Student;

This syntax can also be written as:


SELECT FirstName FN, LastName LN
FROM Student;
However, it is preferable to use the AS command for clarity (it is easier to read than just a
space).
Alias on a table:
SELECT *
FROM Student AS S;

SELECT S.FirstName, S.LastName


FROM Student AS S;

2.4 SQL WHERE


In SQL, the WHERE command is used to filter the results of a query by specifying a ondition.
This condition is typically a logical expression that is evaluated for each row in the table, and
only the rows where the condition is true are included in the final result.
The WHERE command is used in conjunction with a SELECT query as follows:
SELECT column_names
FROM table_name
WHERE condition;

Example:
To display only the students from the city of M’sila, write:
SELECT *
FROM Student
WHERE City = 'Msila';

This will give the following result:

ID FirstName LastName BirthDate City


3 Abir Saoucha 13/08/2011 Msila
4 Sofiane Bakhti 02/01/2010 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:

 <: Less than


 >: Greater than
 <=: Less than or equal to
 >=: Greater than or equal to

 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:

 BETWEEN: Matches a range of values


 NOT BETWEEN: Does not match a range of values

 List Comparison:

 IN: Matches a list of values


 NOT IN: Does not match a list of values

 Null Value Comparison:

 IS NULL: Checks if a value is null


 IS NOT NULL: Checks if a value is not null

2.5. SQL AND & OR

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.

 AND ensures that both condition1 and condition2 are true:

SELECT column_names
FROM table_name
WHERE condition1 AND condition2;

 OR checks if either condition1 or condition2 is true:

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:

Consider the following stock table:

Id Designation Category Price Quantity


1 Armoire Meuble 12500 14
2 Chaise Meuble 6000 35
3 Table Meuble 9000 12
4 Porte Manteau Meuble 7500 25
5 PC Informatique 56000 20
6 Imprimante Informatique 31000 5

SELECT *
FROM produit
WHERE Categorie = 'Meuble' AND Quantite < 20;

This will display the following:

Id Designation Category Price Quantity


1 Armoire Meuble 12500 14
3 Table Meuble 9000 12

SELECT *
FROM produit
WHERE Categorie = 'Informatique' OR Quantite >= 20;

This will display the following:

Id Designation Category Price Quantity


2 Chaise Meuble 6000 35
4 Porte Manteau Meuble 7500 25
5 PC Informatique 56000 20
6 Imprimante Informatique 31000 5
SELECT *
FROM produit
WHERE (Categorie = 'Informatique' AND Quantite < 20)
OR (Categorie = 'Meuble' AND Quantite < 15);

This will display the following:

Id Designation Category Price Quantity


1 Armoire Meuble 12500 14
3 Table Meuble 9000 12
6 Imprimante Informatique 31000 5

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');

Returns the following result:

ID Last_Name First_Name Birth_Date City_St


1 Benslimane Nadir 21/03/2012 Alger
3 Saoucha Abir 13/08/2011 Msila
4 Bakhti Sofiane 02/01/2010 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

WHERE City_St NOT IN ('Alger', 'Msila');


Returns the following result:

ID Last_Name First_Name Birth_Date City_St


2 Hammam Lyes 05/05/2010 Bouira
5 Boubakri Hind 25/11/2012 Setif

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

WHERE City_St = 'Alger' OR City_St = 'Setif' OR City_St = 'Msila';

------------------------

SELECT *

FROM Student

WHERE City_St IN ('Alger', 'Setif', 'Msila');

You might also like