Unit 3. Introduction To SQL
Unit 3. Introduction To SQL
What is SQL?
SQL is a short-form of the structured query language, and it is pronounced as
S-Q-L or sometimes as See-Quell.
You can easily create and manipulate the database, access and modify the
table rows and columns, etc. This query language became the standard of
ANSI in the year of 1986 and ISO in the year of 1987.
Why SQL?
Nowadays, SQL is widely used in data science and analytics. Following are
the reasons which explain why it is widely used:
o The basic use of SQL for data professionals and SQL users is to insert,
update, and delete the data from the relational database.
o SQL allows the data professionals and users to retrieve the data from
the relational database management systems.
o It also helps them to describe the structured data.
o It allows SQL users to create, drop, and manipulate the database and
its tables.
o It also helps in creating the view, stored procedure, and functions in
the relational database.
o It allows you to define the data and modify that stored data in the
relational database.
o It also allows SQL users to set the permissions or constraints on table
columns, views, and stored procedures
Process of SQL
o When we are executing the command of SQL on any Relational
database management system, then the system automatically finds
the best routine to carry out our request, and the SQL engine
determines how to interpret that particular command.
o Structured Query Language contains the following four components in
its process:
o Query Dispatcher
o Optimization Engines
o Classic Query Engine
o SQL Query Engine, etc.
A classic query engine allows data professionals and users to maintain non-
SQL queries. The architecture of SQL is shown in the following diagram:
Some SQL Commands
The SQL commands help in creating and managing the database. The most
common SQL commands which are highly used are mentioned below:
1. CREATE command
2. UPDATE command
3. DELETE command
4. SELECT command
5. DROP command
6. INSERT command
1. SELECT Statement
This SQL statement reads the data from the SQL database and shows it as
the output to the database user.
3. UPDATE Statement
This SQL statement changes or modifies the stored data in the SQL
database.
UPDATE table_name
SET column_name1 = new_value_1, column_name2 = new_value_2, ...., column_na
meN = new_value_N
[ WHERE CONDITION ];
3. DELETE Statement
This SQL statement deletes the stored data from the SQL database.
The above SQL alter statement adds the column with its datatype in the
existing database table.
The above 'SQL alter statement' renames the old column name to the new
column name of the existing database table.
The above SQL alter statement deletes the column of the existing database
table.
This example adds the new field whose name is Designation with size 18 in
the Employee_details table of the SQL database.
The above syntax of the drop statement deletes specified tables completely
if they exist in the database.
The above example deletes the company database from the system.
9. INSERT INTO Statement
This SQL statement inserts the data or records in the existing table of the
SQL database. This statement can easily insert single and multiple records in
a single query statement.
This example inserts 101 in the first column, Akhil in the second
column, Sharma in the third column, 40000 in the fourth column,
and Bangalore in the last column of the table Employee_details.
This example deletes the record of all employees from the Employee_details
table of the database.
DESCRIBE Employee_details;
COMMIT
ROLLBACK
This example deletes the index idx_First_Name from the SQL database.
USE database_name;
Data types mainly classified into three categories for every database
BINARY(Size) It is equal to CHAR() but stores binary byte strings. Its size
parameter specifies the column length in the bytes. Default is
1.
VARBINARY(Size It is equal to VARCHAR() but stores binary byte strings. Its size
) parameter specifies the maximum column length in bytes.
ENUM(val1, It is used when a string object having only one value, chosen
val2, val3,...) from a list of possible values. It contains 65535 values in an
ENUM list. If you insert a value that is not in the list, a blank
value will be inserted.
SET( val1,val2,v It is used to specify a string that can have 0 or more values,
al3,....) chosen from a list of possible values. You can list up to 64
values at one time in a SET list.
BIT(Size) It is used for a bit-value type. The number of bits per value is
specified in size. Its size can be 1 to 64. The default value is 1.
INT(size) It is used for the integer value. Its signed range varies from -
2147483648 to 2147483647 and unsigned range varies from 0 to
4294967295. The size parameter specifies the max display width
that is 255.
The manipulation and retrieving of the data are performed with the help of reserved words and
characters, which are used to perform arithmetic operations, logical operations, comparison
operations, compound operations, etc.
Operator SQL_Operand
In the following table, the operators at the top have high precedence, and
the operators that appear at the bottom have low precedence.
** Exponentiation operator
OR Inclusion operator
For Example,
UPDATE employee
SET salary = 20 - 3 * 5 WHERE Emp_Id = 5;
In the above SQL example, salary is assigned 5, not 85, because the *
(Multiplication)
Types of Operator
SQL operators are categorized in the following categories:
In this query, we have performed the SQL addition operation on the single
column of the given table.
o Suppose, we want to add the Salary and monthly bonus columns of the
above table, then we have to write the following query in SQL:
In this query, we have added two columns with each other of the above
table.
In this query, we have performed the SQL subtraction operation on the single
column of the given table.
In this query, we have multiplied the values of two columns by using the
WHERE clause.
In SQL, we can also divide the numerical values of one column by another
column of the same table by specifying both column names as the first and
second operand.
We can also perform the division operation on the stored numbers in the
column of the SQL table.
In this query, we have performed the SQL division operation on the single
column of the given table.
1 56 4
2 32 8
3 89 9
4 18 10
5 10 5
This operator returns TRUE records from the database table if the value of
both operands specified in the query is matched.
In this example, we used the SQL equal operator with WHERE clause for
getting the records of those employees whose salary is 30000.
This operator returns those records or rows from the database views and
tables if the value of both operands specified in the query is not matched
with each other.
In this example, we used the SQL equal not operator with WHERE clause for
getting the records of those employees whose salary is not 45000.
Here, SQL greater than operator displays the records of those employees
from the above table whose Employee Id is greater than 202.
This comparison operator checks that the left side operand is lesser than the
right side operand. If the condition becomes true, then this operator in SQL
displays the data which is less than the value of the right-side operand.
Here,SQL less than operator with WHERE clause displays the records of
those employees from the above table whose Employee Id is less than 204.
This comparison operator checks that the left side operand is lesser and
equal to the right side operand.
Here, SQL less than equals to the operator with WHERE clause displays
the rows of those employees from the table whose Employee Id is less than
and equals 202.
1. SELECT,
2. HAVING, and
3. WHERE.
Here, we used the SQL ALL operator with greater than the operator.
SQL OR Operator
The OR operator in SQL shows the record from the table if any of the
conditions separated by the OR operator evaluates to True. It is also known
as the conjunctive operator and is used with the WHERE clause.
Syntax of OR operator:
1. SELECT column1, ...., columnN FROM table_Name WHERE condition1 OR condition2
OR condition3 OR ....... OR conditionN;
Here, SQL OR operator with WHERE clause shows the record of employees
whose salary is 25000 or the city is Delhi.
If there is no value in the given range, then this operator shows NULL value.
Syntax of BETWEEN operator:
Here, we used the SQL BETWEEN operator with the Emp_Salary field.
SQL IN Operator
The IN operator in SQL allows database users to specify two or more values
in a WHERE clause. This logical operator minimizes the requirement of
multiple OR conditions.
This operator makes the query easier to learn and understand. This operator
returns those rows whose values match with any value of the given list.
Syntax of IN operator:
1. SELECT column1, column2 ...., columnN FROM table_Name WHERE NOT condition;
In this example, we used the SQL NOT operator with the Emp_City column.
o Suppose, we want to show all the information of those employees from
the Employee_details table whose Cityis not Delhi and Chandigarh. For this,
we have to write the following query in SQL:
1. SELECT * FROM Employee_details WHERE NOT Emp_City = 'Delhi' AND NOT Emp_Cit
y = 'Chandigarh';
In this example, we used the SQL NOT operator with the Emp_City column.
The ANY logical operator must match at least one record in the inner query
and must be preceded by any SQL comparison operator.
The percentage (%) sign is a wildcard which is used in conjunction with this
logical operator.
This operator is used in the WHERE clause with the following three
statements:
1. SELECT statement
2. UPDATE statement
3. DELETE statement
In this example, we used the SQL LIKE operator with Emp_Name column
because we want to access the record of those employees whose name
starts with s.
Set operators combine more than one select statement in a single query and
return a specific result set.
Following are the various set operators which are performed on the
similar data stored in the two SQL database tables:
The data type and the number of columns must be the same for each
SELECT statement used with the UNION operator. This operator does not
show the duplicate records in the output table.
Table: Employee_details1
Table: Employee_details2
Table: Employee_details1
Table: Employee_details2
Table: Employee_details1
Table: Employee_details2
Suppose, we want to see a common record of the employee from both the
tables in a single output. For this, we have to write the following query in
SQL:
MINUS
SELECT column1, column2 ...., columnN FROM Second_tablename [WHERE condition
s];
Table: Employee_details1
Table: Employee_details2
Suppose, we want to see the name of employees from the first result set
after the combination of both tables. For this, we have to write the following
query in SQL:
These types of operators can be easily operated on the numeric data value
of the SQL table.
o Suppose, we want to see the salary of each employee as positive from the
Employee_details table. For this, we have to write the following query in SQL:
o Suppose, we want to see the salary of each employee as negative from the
Employee_details table. For this, we have to write the following query in SQL:
1. SELECT column1 & column2 & .... & columnN FROM table_Name [WHERE conditions]
;
This example consists of the following table, which has two columns. Each
column holds numerical values.
When we use the Bitwise AND operator in SQL, then SQL converts the values
of both columns in binary format, and the AND operation is performed on the
converted bits.
After that, SQL converts the resultant bits into user understandable format,
i.e., decimal format.
Column1 Column2
1 1
2 5
3 4
4 2
5 3
o Suppose, we want to perform the Bitwise AND operator between both the
columns of the above table. For this, we have to write the following query in
SQL:
1. SELECT Column1 & Column2 From TABLE_AND ;
Bitwise OR (|)
The Bitwise OR operator performs the logical OR operation on the given
Integer values. This operator checks each bit of a value with the
corresponding bit of another value.
This example consists of a table that has two columns. Each column holds
numerical values.
When we used the Bitwise OR operator in SQL, then SQL converts the values
of both columns in binary format, and the OR operation is performed on the
binary bits. After that, SQL converts the resultant binary bits into user
understandable format, i.e., decimal format.
Column1 Column2
1 1
2 5
3 4
4 2
5 3
The database developers and the users use this statement in SQL for
creating the new database in the database systems. It creates the database
with the name which has been specified in the Create Database statement.
Following are the most important points which are required to learn while
creating a database:
o The database we want to create should be a simple and unique name, which
can be easily identified.
o Database name should be no more than 128 characters.
Example 1:
This example creates the Student database. To create the Student
database, you have to type the following command in Structured Query
Language:
When this query is executed successfully, then it will show the following
output:
You can also verify that your database is created in SQL or not by using the
following query:
SHOW DATABASE ;
SQL does not allow developers to create the database with the existing
database name. Suppose if you want to create another Student database in
the same database system, then the Create Database statement will show
the following error in the output:
So, firstly you have to delete the existing database by using the Drop
Statement. You can also replace the existing database with the help of
Replace keyword.
If you want to replace the existing Student database, then you have to type
the following SQL query:
Example 2:
When this query is executed successfully, then it will show the following
output:
SHOW DATABASE ;
We know that SQL does not allow developers to create the database with the
existing database name.
Following are the most important points which are required to learn before
removing the database from the database system:
o This statement deletes all the data from the database. If you want to restore
the deleted data in the future, you should keep the backup of data of that
database which you want to delete.
o Another most important point is that you cannot delete that database from
the system which is currently in use by another database user. If you do so,
then the drop statement shows the following error on screen:
In this SQL syntax, we have to specify the name of that database which we
want to delete permanently from the database system. We have to write the
name of the database after the DROP DATABASE keyword in every example.
We can also delete multiple databases easily by using the single DROP
syntax:
Example1:
Suppose, we want to delete the Student database with all its data from the
database system so, firstly we have to check that the Student database
exists in the system or not by using the following statement:
SHOW DATABASES ;
If the Student database is shown in the output, then we have to type the
following query in SQL for removing the Student database
If the Student database does not exist in the database system and we run
the above query in SQL, then the query will show the following output.
xample2:
Suppose, we want to delete the College database with all its tables and
views from the database system, firstly we have to check that if the College
database exists in the system or not by using the following statement:
SHOW DATABASES;
If the College database is shown in the output, then you have to type the
following query in SQL for removing the College database permanently: