Introduction To SQL - 2023
Introduction To SQL - 2023
Introduction
There are many RDBMS such as MySQL, Microsoft SQL Server, PostgreSQL, Oracle, etc.
that allow us to create a database consisting of relations and to link one or more relations for
efficient querying to store, retrieve and manipulate data on that database. In this chapter, we
will learn how to create, populate and query database using MySQL Software and SQL
language.
One has to write application programs to access data in case of a file system. However, for
database management systems there are special kind of programming languages called query
language that can be used to access data from the database. The Structured Query Language
(SQL) is the most popular query language used by major relational database management
systems such as MySQL, ORACLE, SQL Server, etc. SQL is a database computer language
designed for managing data in relational database management systems (RDBMS).
SQL, is a standardized computer language that was originally developed by IBM for querying,
altering and defining relational databases, using declarative statements. SQL is easy to learn as
the statements comprise of descriptive English words and are not case sensitive. We can create
and interact with a database using SQL in an efficient and easy way. The benefit with SQL is
that we don’t have to specify how to get the data from the database. Rather, we simply specify
what is to be retrieved, and SQL does the rest. Although called a query language, SQL can do
much more besides querying. SQL provides statements for defining the structure of the data,
manipulating data in the database, declare constraints and retrieve data from the database in
various ways, depending on our requirements.
In this chapter, we will learn how to create a database using MySQL as the RDBMS software.
We know that a database consists of one or more relations and each relation (table) is made up
of attributes (column). Each attribute has a data type. We can also specify constraints for each
attribute of a relation.
Specifies character type data of length ‘n’ where n could be any value from
VARCHAR(n) 0 to 65535. But unlike CHAR, VARCHAR is a variablelength data type.
That is, declaring VARCHAR (30) means a maximum of 30 characters can
be stored but the actual allocated bytes will depend on the length of entered
string. So ‘city’ in VARCHAR (30) will occupy the space needed to store 4
characters only.
INT specifies an integer value. Each INT value occupies 4 bytes of storage.
INT The range of values allowed in integer type are -2147483648 to
2147483647. For values larger than that, we have to use BIGINT, which
occupies 8 bytes.
Holds numbers with decimal points. Each FLOAT value occupies 4 bytes.
FLOAT
The DATE type is used for dates in 'YYYY-MM-DD' format. YYYY is the
DATE 4 digit year, MM is the 2 digit month and DD is the 2 digit date. The
supported range is '1000-01-01' to '9999-12-31'.
2.2. Constraints
Constraints are certain types of restrictions on the data values that an attribute can have. They
are used to ensure the accuracy and reliability of data. However, it is not mandatory to define
constraint for each attribute of a table. Table 2 lists various SQL constraints.
Constraint Description
NOT NULL Ensures that a column cannot have NULL values where NULL means
missing/ unknown/not applicable value.
PRIMARY The column which can uniquely identify each row or record in a table.
KEY
FOREIGN The column which refers to value of an attribute defined as primary key in
KEY another table.
If CUSTOMERS table has already been created, then to add a NOT NULL constraint to
SALARY column in Oracle and MySQL, you would write a statement similar to the
following:
If CUSTOMERS table has already been created, then to add a DFAULT constraint to
SALARY column, you would write a statement similar to the following:
4
ALTER COLUMN SALARY DROP DEFAULT;
Example:
For example, the following SQL creates a new table called CUSTOMERS and adds five
columns. Here, AGE column is set to UNIQUE, so that you can not have two records with
same age:
If CUSTOMERS table has already been created, then to add a UNIQUE constraint to AGE
column, you would write a statement similar to the following:
You can also use the following syntax, which supports naming the constraint in multiple
columns as well:
If you are using MySQL, then you can use the following syntax:
Note: You would use these concepts while creating database tables.
To create a PRIMARY KEY constraint on the "ID" column when CUSTOMERS table
already exists, use the following SQL syntax:
NOTE: If you use the ALTER TABLE statement to add a primary key, the primary key
column(s) must already have been declared to not contain NULL values (when the table was
first created).
For defining a PRIMARY KEY constraint on multiple columns, use the following SQL
syntax:
6
);
To create a PRIMARY KEY constraint on the "ID" and "NAMES" columns when
CUSTOMERS table already exists, use the following SQL syntax:
A foreign key is a key used to link two tables together. This is sometimes called a referencing
key.
The relationship between 2 tables matches the Primary Key in one of the tables with a
Foreign Key in the second table.
If a table has a primary key defined on any field(s), then you cannot have two records having
the same value of that field(s).
Example:
Consider the structure of the two tables as follows:
CUSTOMERS table:
CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
ORDERS table:
CREATE TABLE ORDERS (
ID INT NOT NULL,
7
DATE DATETIME,
CUSTOMER_ID INT references CUSTOMERS(ID),
AMOUNT double,
PRIMARY KEY (ID)
);
If ORDERS table has already been created, and the foreign key has not yet been, use the
syntax for specifying a foreign key by altering a table.
Example:
For example, the following SQL creates a new table called CUSTOMERS and adds five
columns. Here, we add a CHECK with AGE column, so that you cannot have any
CUSTOMER below 18 years:
8
If CUSTOMERS table has already been created, then to add a CHECK constraint to AGE
column, you would write a statement similar to the following:
You can also use following syntax, which supports naming the constraint and multiple
columns as well:
Data definition starts with the create statement. This statement is used to create a database and
its tables (relations). Before creating a database, we should be clear about the number of
tables in the database, the columns (attributes) in each table along with the data type of each
column. This is how we decide the relation schema.
To create a database, we use the CREATE DATABASE statement as shown in the following
syntax:
CREATE DATABASE databasename;
9
After creating database Studentdb, we need to define relations (create tables) in this database
and specify attributes for each relation along with data types for each attribute. This is done
using the CREATE TABLE statement. Syntax:
CREATE TABLE tablename(
attributename1 datatype constraint,
attributename2 datatype constraint,
:
AttributenameN datatype constraint);
It is important to observe the following points with respect to the Create Table statement:
- N is the degree of the relation, means there are N columns in the table.
- Attribute name specifies the name of the column in the table.
- Datatype specifies the type of data that an attribute can hold.
- Constraint indicates the restrictions imposed on the values of an attribute. By default,
each attribute can take NULL values except for the primary key.
Example :
After creating a table we may realize that we need to add/remove an attribute or to modify the
datatype of an existing attribute or to add constraint in attribute. In all such cases, we need to
change or alter the structure of the table by using the alter statement.
Syntax:
ALTER TABLE tablename ADD/Modify/DROP attribute1, attribute2,..
Sometimes, we may need to add an additional attribute in a table. It can be done using the
syntax given below:
10
3.4. Drop Statement
Sometimes a table in a database or the database itself needs to be removed. We can use DROP
statement to remove a database or a table permanently from the system. However, one should
be very cautious while using this statement as it cannot be undone.
Syntax to drop a table:
DROP TABLE table_name;
Syntax to drop a database:
DROP DATABASE database_name;
Cautions:
1) Using the Drop statement to remove a database will ultimately remove all the tables
within it.
2) DROP statement will remove the tables or database created by you. Hence you may
apply DROP statement at the end of the chapter.
In the previous section, we created the database Studentdb having two relations STUDENT,
and depatment. When we create a table, only its structure is created but the table has no data.
To populate records in the table, INSERT statement is used. Similarly, table records can be
deleted or updated using SQL data manipulation statements.
Data Manipulation using a database means either retrieval (access) of existing data, insertion
of new data, removal of existing data or modification of existing data in the database.
INSERT INTO statement is used to insert new records in a table. Its syntax is:
INSERT INTO tablename
VALUES(value 1, value 2,....);
Here, value 1 corresponds to attribute 1, value 2 corresponds to attribute 2 and so on. Note
that we need not to specify attribute names in insert statement if there are exactly same
number of values in the INSERT statement as the total number of attributes in the table.
11
Example : insert into department values (1, « History ») ;
Caution: While populating records in a table with foreign key, ensure that records in
referenced tables are already populated.
Example : insert into students values (« 12UYT », « peupie », « Guy »,12,1, « M » ) ;
If we want to provide values only for some of the attributes in a table (supposing other
attributes having NULL or any other default value), then we shall specify the attribute name
alongside each data value as shown in the following syntax of INSERT INTO statement.
Syntax:
INSERT INTO tablename (column1, column2, ...) VALUES (value1, value2, ...);
Example : TO-DO
So far we have learnt how to create database as well as to store and manipulate data. We are
interested to store data in a database as it is easier to retrieve data in future from databases in
whatever way we want. The Structured Query Language (SQL) has efficient mechanisms to
retrieve data stored in multiple tables in a MySQL database (or any other RDBMS). The user
enters the SQL commands called queries where the specific requirements for data to be
retrieved are provided. The SQL statement SELECT is used to retrieve data from the tables in
a database and is also called query statement.
a) SELECT Statement
The SQL statement SELECT is used to retrieve data from the tables in a database and the
output is also displayed in tabular form.
Syntax:
SELECT attribute1, attribute2, ...
FROM table_name
WHERE condition
Here, attribute1, attribute2, ... are the column names of the table table_name from which we
want to retrieve data. The FROM clause is always written with SELECT clause as it specifies
the name of the table from which data is to be retrieved. The WHERE clause is optional and is
used to retrieve data that meet specified condition(s).
Example : To display the name and gender of student with matricule number «454TR » , we
write the following query:
b) ORDER BY statement
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in descending order, use the DESC keyword.
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Updation and deletion of data are also the parts of SQL data manipulation. In this section, we
are going to apply these two data manipulation methods.
We may need to make changes in the value(s) of one or more columns of existing records in a
table. For example, we may require some changes in address, phone number or spelling of
name, etc. The UPDATE statement is used to make such modifications in the existing data.
The DELETE statement is used to delete one or more record(s) from a table.
Syntax:
DELETE FROM table_name
WHERE condition;
13
Example : TO-DO.
Caution: Like UPDATE statement, we need to be careful to include WHERE clause while
using DELETE statement to delete records in a table. Otherwise, all the records in the table
will get deleted.
5. SQL Joins
The SQL Joins clause is used to combine records from two or more tables in a database. A
JOIN is a
means for combining fields from two tables by using values common to each.
Consider the following two tables,
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|3| kaushik |23|Kota|2000.00|
|4|Chaitali|25|Mumbai|6500.00|
|5|Hardik|27|Bhopal|8500.00|
|6|Komal|22| MP |4500.00|
|7|Muffy|24|Indore|10000.00|
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+----------------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+------------------+-------------+-------------------+
|102|2009-10-08|3|3000|
|100|2009-10-08|3|1500|
|101|2009-11-20|2|1560|
|103|2008-05-20|4|2060|
+-----+---------------------+-------------+---------------+
Now, let us join these two tables in our SELECT statement as follows:
+----+----------+-----+-----------------+
| ID | NAME | AGE | AMOUNT |
+----+----------+-----+----------------+
|3| kaushik |23|3000|
|3| kaushik |23|1500|
|2|Khilan|25|1560|
|4|Chaitali|25|2060|
+----+----------+-----+----------------+
Here, it is noticeable that the join is performed in the WHERE clause. Several operators can
be used to join tables, such as =, <, >, <>, <=, >=, !=, BETWEEN, LIKE, and NOT; they can
all be used to join tables. However, the most common operator is the equal symbol.
The most frequently used and important of the joins is the INNER JOIN. They are also
referred to as an EQUIJOIN.
The INNER JOIN creates a new result table by combining column values of two tables
(table1 and table2) based upon the join-predicate. The query compares each row of table1
with each row of table2 to find all pairs of rows which satisfy the join-predicate. When the
join-predicate is satisfied, column values for each matched pair of rows of A and B are
combined into a result row.
Syntax:
The basic syntax of INNER JOIN is as follows:
15
Example:
Consider the following two tables, (a) CUSTOMERS table is as follows:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|3| kaushik |23|Kota|2000.00|
|4|Chaitali|25|Mumbai|6500.00|
|5|Hardik|27|Bhopal|8500.00|
|6|Komal|22| MP |4500.00|
|7|Muffy|24|Indore|10000.00|
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+-------------+
| OID | DATE | ID | AMOUNT |
+-----+---------------------+-------------+--------------+
|102|2009-10-08|3|3000|
|100|2009-10-08|3|1500|
|101|2009-11-20|2|1560|
|103|2008-05-20|4|2060|
+-----+---------------------+-------------+--------+
Now, let us join these two tables using INNER JOIN as follows:
+----+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+---------------------+
|3| kaushik |3000|2009-10-08|
|3| kaushik |1500|2009-10-08|
|2|Khilan|1560|2009-11-20|
|4|Chaitali|2060|2008-05-20|
+----+----------+--------+---------------------+
16
5.2. LEFT JOIN
The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the
right table. This means that if the ON clause matches 0 (zero) records in right table, the join
will still return a row in the result, but with NULL in each column from right table.
This means that a left join returns all the values from the left table, plus matched values from
the right table or NULL in case of no matching join predicate.
Syntax:
The basic syntax of LEFT JOIN is as follows:
Here given condition could be any given expression based on your requirement.
Example:
Consider the following two tables, (a) CUSTOMERS table is as follows:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|3| kaushik |23|Kota|2000.00|
|4|Chaitali|25|Mumbai|6500.00|
|5|Hardik|27|Bhopal|8500.00|
|6|Komal|22| MP |4500.00|
|7|Muffy|24|Indore|10000.00|
+----+----------+-----+-----------+----------+
+-----+---------------+-------------+---------------------+
| OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------+-------------+---------------------+
|102|2009-10-08|3|3000|
|100|2009-10-08|3|1500|
|101|2009-11-20|2|1560|
|103|2008-05-20|4|2060|
+-----+---------------------+-------------+--------+
Now, let us join these two tables using LEFT JOIN as follows:
+----+----------+--------+---------------+
| ID | NAME | AMOUNT | DATE |
+----+----------+--------+--------------+
|1|Ramesh| NULL| NULL |
|2|Khilan|1560|2009-11-20|
|3| kaushik |3000|2009-10-08|
|3| kaushik |1500|2009-10-08|
|4|Chaitali|2060|2008-05-20|
|5|Hardik| NULL | NULL |
|6|Komal| NULL | NULL |
|7|Muffy| NULL | NULL |
+----+----------+--------+--------------+
The SQL RIGHT JOIN returns all rows from the right table, even if there are no matches in
the left table. This means that if the ON clause matches 0 (zero) records in left table, the join
will still return a row in the result, but with NULL in each column from left table.
This means that a right join returns all the values from the right table, plus matched values
from the left table or NULL in case of no matching join predicate.
Syntax:
The basic syntax of RIGHT JOIN is as follows:
Example:
Consider the following two tables, (a) CUSTOMERS table is as follows:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRE| SALARY |
+----+----------+-----+-----------+-------------+
|1|Ramesh|32|Ahmedabad|2000.00|
|2|Khilan|25|Delhi|1500.00|
|3| kaushik |23|Kota|2000.00|
|4|Chaitali|25|Mumbai|6500.00|
|5|Hardik|27|Bhopal|8500.00|
|6|Komal|22| MP |4500.00|
|7|Muffy|24|Indore|10000.00|
18
+----+----------+-----+-----------+----------+
+-----+-------------+-------------+-----------------------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+--------------+--------------------+----------------+
|102|2009-10-08|3|3000|
|100|2009-10-08|3|1500|
|101|2009-11-20|2|1560|
|103|2008-05-20|4|2060|
+-----+---------------------+-------------+--------+
Now, let us join these two tables using RIGHT JOIN as follows:
+------+----------+--------+--------------+
| ID | NAME | AMOUNT | DAT |
+------+----------+--------+--------------+
|3| kaushik |3000|2009-10-08|
|3| kaushik |1500|2009-10-08|
|2|Khilan|1560|2009-11-20|
|4|Chaitali|2060|2008-05-20|
+------+----------+--------+-------------+
6. SQL Functions
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
19
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
The COUNT() function returns the number of rows and as well all the rows that matches a
specified criterion.
COUNT() Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
COUNT() Syntax to select all rows
SELECT COUNT(*)(column_name)
FROM table_name
WHERE condition;
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
20