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

Unit_3 Introduction to SQL (Structured Query Language)

This document provides an introduction to Structured Query Language (SQL), covering its characteristics, advantages, disadvantages, and types of statements such as DDL, DML, DCL, and TCL. It details SQL's role in managing relational databases, including data types, basic commands, and practical examples for creating and manipulating tables. Additionally, it discusses the SQL process, its advantages like faster query processing and security, as well as disadvantages such as complexity and limited flexibility.

Uploaded by

stoke7428
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unit_3 Introduction to SQL (Structured Query Language)

This document provides an introduction to Structured Query Language (SQL), covering its characteristics, advantages, disadvantages, and types of statements such as DDL, DML, DCL, and TCL. It details SQL's role in managing relational databases, including data types, basic commands, and practical examples for creating and manipulating tables. Additionally, it discusses the SQL process, its advantages like faster query processing and security, as well as disadvantages such as complexity and limited flexibility.

Uploaded by

stoke7428
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Database Management System

Unit-3 Introduction to Structured Query Language (SQL)

INDEX
Unit-III Introduction to SQL: [Weightage=25% approx., Lectures=8, Practical’s= 16]:
Overview, Characteristics of SQL, Advantage of SQL, SQL data types and literals. Basics of
SQL Types of SQL Statements: DDL, DML, DCL, TCL.
Basic SQL Queries: SQL operators, Creating Database, Creating, Modifying and Deleting
Tables, Queries: Insert, Select, Update, Delete, Where Clause, Order By.

X ---------------- X

Page 1 of 33
❖ Overview:
➢ SQL stands for Structured Query Language.
➢ It is used for storing and managing data in relational database
management system (RDMS).
➢ It is a standard language for Relational Database System.
➢ It enables a user to create, read, update, and delete relational databases
and tables.
➢ All the RDBMS like MySQL, Oracle, and SQL Server use SQL as their
standard database language.
➢ SQL allows users to query the database in a number of ways, using
English-like statements.

▪ SQL Process:

➢ When an SQL command is executing for any RDBMS, then the


system figure out the best way to carry the request and the SQL
engine determines that how to interpret the task.
➢ In the process, various components are included. These components
can be optimization Engine, Query engine, Query dispatcher,
classic, etc.
➢ All the non-SQL queries are handled by the classic query engine,
but SQL query engine won't handle logical files.

Page 2 of 33
❖ Characteristics of SQL:

➢ SQL is easy to learn.


➢ Structured Query Language (SQL) is a standard language to write
queries.
➢ It was developed under R Project by IBM.
➢ SQL has a basic grammar and syntax. It was declared as a standard
language to use by American Standard National Institute (ANSI) and
International Standard Organization (ISO).
➢ The functionality of SQL language is virtually similar across the
operating system platforms.
➢ Using SQL doesn’t require programming experience, but programming
experience helps one to conceptualize what a particular SQL command
will help to execute and retrieve SQL queries.
➢ The ANSI SQL provides with:
➢ Specific syntax and semantics of SQL data definition and data
manipulation languages.
➢ It also provides with basic data structure and operations for designing,
assessing, maintaining, controlling and protecting SQL databases.
➢ Portability of database definition and application is also provided.
Applications can be moved from one machine to another.
➢ IS professionals share a common language and reduce training costs.
➢ Professionals can become proficient in its use and increase the
productivity.
➢ It provides with longevity.
➢ It provides with reduced dependence on single vendor.
➢ SQL is used to access data from relational database management
systems.
➢ SQL can execute queries against the database.
➢ SQL is used to describe the data.
➢ SQL is used to define the data in the database and manipulate it when
needed.
➢ SQL is used to create and drop the database and tables.
➢ SQL is used to create a view, stored procedure, function in a database.
➢ SQL allows users to set permissions on tables, procedures, and views.

Page 3 of 33
▪ DBMS can be classified into two types:
▪ Relational Database Management System (RDBMS) and
▪ Non-Relational Database Management System. (NoSQL or Non-
SQL)
➢ RDBMS: Data is organized in the form of tables and each table has a
set of rows and columns. The data are related to each other through
primary and foreign keys.
➢ NoSQL: Data is organized in the form of key-value pairs, documents,
graphs, or column-based. These are designed to handle large-scale,
high-performance scenarios.

❖ Advantage of SQL:

➢ SQL has many advantages which make it popular and highly


demanded. It is a reliable and efficient language used for
communicating with the database.

➢ Some advantages of SQL are as follows:

➢ Faster Query Processing:


▪ Large amount of data is retrieved quickly and efficiently.
Operations like Insertion, deletion, manipulation of data is also
done in almost no time.
➢ No Coding Skills:
▪ For data retrieval, large number of lines of code is not required.
All basic keywords such as SELECT, INSERT INTO, UPDATE,
etc are used and also the syntactical rules are not complex in
SQL, which makes it a user-friendly language.
➢ Standardized Language:
▪ Due to documentation and long establishment over years, it
provides a uniform platform worldwide to all its users.
➢ Portable:
▪ It can be used in programs in PCs, server, laptops independent of
any platform (Operating System, etc). Also, it can be embedded
with other applications as per need/requirement/use.
Page 4 of 33
➢ Interactive Language:
▪ Easy to learn and understand, answers to complex queries can be
received in seconds.
➢ Multiple data views:
▪ In SQL, you can create multiple data views to provide different
perspectives on the underlying data.
▪ A view is a virtual table based on the result of a SELECT query.
▪ Views allow you to encapsulate complex queries and present a
simplified or specific subset of data to users.
➢ Scalability:
▪ SQL databases can handle large volumes of data and can be
scaled up or down as per the requirements of the application.
➢ Security:
▪ SQL databases have built-in security features that help protect
data from unauthorized access, such as user authentication,
encryption, and access control.
➢ Data Integrity:
▪ SQL databases enforce data integrity by enforcing constraints
such as unique keys, primary keys, and foreign keys, which help
prevent data duplication and maintain data accuracy.
➢ Backup and Recovery:
▪ SQL databases have built-in backup and recovery tools that help
recover data in case of system failures, crashes, or other disasters.
➢ Data Consistency:
▪ SQL databases ensure consistency of data across multiple tables
through the use of transactions, which ensure that changes made
to one table are reflected in all related tables.

Page 5 of 33
❖ Disadvantage of SQL:

➢ Although SQL has many advantages, still there are a few


disadvantages.

➢ Various Disadvantages of SQL are as follows:

➢ Complex Interface:
▪ SQL has a difficult interface that makes few users uncomfortable
while dealing with the database.
➢ Cost:
▪ Some versions are costly and hence, programmers cannot access it.
➢ Partial Control:
▪ Due to hidden business rules, complete control is not given to the
database.
➢ Limited Flexibility:
▪ SQL databases are less flexible than NoSQL databases when it
comes to handling unstructured or semi-structured data, as they
require data to be structured into tables and columns.
➢ Lack of Real-Time Analytics:
▪ SQL databases are designed for batch processing and do not
support real-time analytics, which can be a disadvantage for
applications that require real-time data processing.
➢ Limited Query Performance:
▪ SQL databases may have limited query performance when
dealing with large datasets, as queries may take longer to process
than in-memory databases.
➢ Complexity:
▪ SQL databases can be complex to set up and manage, requiring
skilled database administrators to ensure optimal performance
and maintain data integrity.

Page 6 of 33
❖ SQL Data Types and Literals:

➢ An SQL developer must be aware of what type of data will be stored


inside each column while creating a table.
➢ The data type guideline for SQL is to understand what type of data is
expected inside each column and it also identifies how SQL will
interact with the stored data.
➢ For example, in a particular column of a table, if we want to store a
string type of data then we will have to declare a string data type of this
column.
➢ Data types mainly classified into three categories for every database.

▪ String Data types.


▪ Numeric Data types.
▪ Date and time Data types.

➢ Data Types in MySQL, SQL Server, and Oracle Databases.


➢ MySQL Data Types. A list of data types used in MySQL database. This
is based on MySQL 8.0.

Page 7 of 33
➢ MySQL String Data Types and Literals.

Page 8 of 33
➢ MySQL Numeric Data Types and Literals.

Page 9 of 33
➢ MySQL Date and Time Data Types and Literals.

▪ Rules to write commands:

➢ SQL follows the following rules:


➢ Structure query language is not case sensitive. Generally, keywords
of SQL are written in uppercase.
➢ Statements of SQL are dependent on text lines. We can use a single
SQL statement on one or multiple text lines.
➢ Using the SQL statements, you can perform most of the actions in a
database.
➢ SQL depends on tuple relational calculus and relational algebra.
➢ Table names cannot exceed 20 characters.
➢ The name of the table must be unique.
➢ Field names also must be unique.
➢ The field list and filed length must be enclosed in parentheses.
➢ The user must specify the field length and type.
➢ The field definitions must be separated with commas.
➢ SQL statements must end with a semicolon.

Page 10 of 33
❖ Basics of SQL Types of SQL Statements: DDL, DML,
DCL, TCL.

➢ Database Languages:
▪ Data Definition Language (DDL).
▪ Data Manipulation Language (DML).
▪ Data Control Language (DCL).
▪ Transactional Control Language (TCL).

➢ Data Definition Language (DDL):


▪ DDL is the short name for Data Definition Language, which
deals with database schemas and descriptions of how the data
should reside in the database.

➢ Data Definition Language (DDL) Commands:


CREATE: to create a database and its objects like (table, index, views, store
procedure, function, and triggers)
ALTER: alters the structure of the existing database
TRUNCATE: remove all records from a table, including all spaces allocated for the
records are removed
RENAME: rename an object
DROP: delete objects from the database

Page 11 of 33
➢ Before learning DDL Commands you need to know basic
Database Commands.
➢ These steps must be followed before writing the database query in
the console screen (Shell) of "XAMPP!" Software.

Step 1: Download Xampp Software.


Step 2: Start Apache.
Step 3: Start MySQL.
Step 4: Click ON Shell.
Step 5: Now Run These Commands One by One.
> cd mysql/bin
> mysql -u root -p
Enter password:
Step 6: A message will be printed automatically if the user logins successfully.

Welcome to the MariaDB monitor. Commands end with ; or \g.


Your MariaDB connection id is 2
Server version: 10.1.34-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database BPCCS;


Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use BPCCS;
Database changed
MariaDB [BPCCS]> show databases;
MariaDB [BPCCS]> show tables;
➢ How to show table details.
Syntax –
➢ DESCRIBE table_name;
MariaDB [BPCCS]> DESCRIBE Section_I;
➢ How to show table details.
Syntax –
➢ SHOW COLUMNS FROM table_name;
MariaDB [BPCCS]> SHOW COLUMNS FROM Section_I;
MariaDB [BPCCS]> drop database BPCCS;

Page 12 of 33
➢ Data Definition Language (DDL) Commands Syntax:

➢ Command-1:
CREATE :
This command is used to create a new table in SQL. The user has to give information like table
name, column names, and their datatypes.

Syntax –
CREATE TABLE table_name
(
column_1 datatype,
column_2 datatype,
column_3 datatype,
....
);

Example –
We need to create a table for storing Student information of a particular Division. Create syntax
would be as below.

MariaDB [BPCCS]> CREATE TABLE Section_I


(
Sr_NO INT(2),
Enrollment_NO VARCHAR(10) PRIMARY KEY,
Roll_NO VARCHAR(10),
Name_of_the_student VARCHAR(30)
);

Output:
Query OK, 0 rows affected (1.828 sec)

Page 13 of 33
➢ Command-2:
ALTER :
This command is used to add, delete or change columns in the existing table. The user needs to
know the existing table name and can do add, delete or modify tasks easily.

Syntax –
Syntax to add a column to an existing table.

ALTER TABLE table_name


ADD column_name datatype;

Example –
In our Section_I table, we want to add a new column for CGPA. The syntax would be as below
as follows.

MariaDB [BPCCS]> ALTER TABLE Section_I


ADD CGPA DECIMAL (4,2); -- Adjust the precision and scale based on your requirements

Output:
Query OK, 0 rows affected (0.568 sec)
Records: 0 Duplicates: 0 Warnings: 0

Page 14 of 33
➢ Command-3:
TRUNCATE :
This command is used to remove all rows from the table, but the structure of the table still exists.

Syntax –
Syntax to remove an existing table.

TRUNCATE TABLE table_name;

Example –
The College Authority wants to remove the details of all students for new batches but wants to
keep the table structure. The command they can use is as follows.

MariaDB [BPCCS]> TRUNCATE TABLE Section_I;


Output:
Query OK, 0 rows affected (0.555 sec)

➢ Command-4:
RENAME:
It is possible to change name of table with or without data in it using simple RENAME
command.
We can rename any table object at any point of time.

Syntax –
RENAME TABLE <Table Name> To <New_Table_Name>;

Example:
If you want to change the name of the table from Section_I to Division_I we can use rename
command as..

MariaDB [BPCCS]> RENAME TABLE Section_I To Division_I;


Output:
Query OK, 0 rows affected (0.568 sec)

Page 15 of 33
➢ Command-5:
DROP:
This command is used to remove an existing table along with its structure from the Database.

Syntax –
Syntax to drop an existing table.

DROP TABLE table_name;


Example –
If the College Authority wants to change their Database by deleting the Division_I Table.

MariaDB [BPCCS]> DROP TABLE Division_I;


Query OK, 0 rows affected (0.220 sec)

Page 16 of 33
➢ Data Manipulation Language (DML):
▪ DML is the short name for Data Manipulation Language which
deals with data manipulation and includes most common SQL
statements such SELECT, INSERT, UPDATE, DELETE, etc.,
and it is used to store, modify, retrieve, delete, and update data in
a database.

➢ Data Manipulation Language (DML) Commands:


SELECT: retrieve data from a database
INSERT: insert data into a table
UPDATE: updates existing data within a table
DELETE: Delete all records from a database table

Page 17 of 33
➢ Data Manipulation Language (DML) Commands Syntax:

➢ Command-1:
SELECT DML Command:
▪ SELECT is the most important data manipulation command in Structured Query
Language.
▪ The SELECT command shows the records of the specified table. It also shows the
particular record of a particular column by using the WHERE clause.
Syntax –
SELECT column_Name_1, column_Name_2, ….., column_Name_N FROM
Name_of_table;

▪ Here, column_Name_1, column_Name_2, ….., column_Name_N are the names


of those columns whose data we want to retrieve from the table.
▪ If we want to retrieve the data from all the columns of the table, we have to use
the following SELECT command:
Syntax –
SELECT * FROM table_name;
Example –
Examples of SELECT Command
Example 1: This example shows all the values of every column from the table.

MariaDB [BPCCS]>SELECT * FROM Section_I;


2 rows in set (0.001 sec)

Page 18 of 33
➢ Command-2:
INSERT DML Command:
▪ INSERT is another most important data manipulation command in
Structured Query Language, which allows users to insert data in database
tables.
Syntax –
INSERT INTO TABLE_NAME
( column_Name1, column_Name2, column_Name3, .... column_NameN ) VALUES
(value_1, value_2, value_3, .... value_N) ;
Example –
This example describes how to insert the record in the database table.
Let's take the following Section_I table, which consists of only 3 records of the student.

MariaDB [BPCCS]>INSERT INTO Section_I (Sr_No, Enrollment_NO, Roll_NO,


Name_of_the_student, CGPA) VALUES
(37, '23BCA04291', '23I1037', 'PATEL VRAJ BHARATBHAI', 8.06);
Query OK, 1 row affected (0.060 sec)

MariaDB [BPCCS]>INSERT INTO Section_I (Sr_No, Enrollment_NO, Roll_NO,


Name_of_the_student, CGPA) VALUES
(38, '23BCA04300', '23I1038', 'PATIL ANJALI LOTANBHAI', 8.03);
Query OK, 1 row affected (0.060 sec)

MariaDB [BPCCS]>INSERT INTO Section_I (Sr_No, Enrollment_NO, Roll_NO,


Name_of_the_student, CGPA) VALUES
(39, '23BCA04317', '23I1039', 'PRAJAPATI KEVAL RAJENRAKUMAR', 7.03);
Query OK, 1 row affected (0.060 sec)

Page 19 of 33
➢ Command-3:
UPDATE DML Command:
▪ UPDATE is another most important data manipulation command in
Structured Query Language, which allows users to update or modify the
existing data in database tables.
▪ Here, 'UPDATE', 'SET', and 'WHERE' are the SQL keywords, and
'Table_name' is the name of the table whose values you want to update.
Syntax –
UPDATE Table_name SET [column_name1= value_1, ….., column_nameN = value_N]
WHERE CONDITION;

Example –
Suppose you want to update the CGPA of the student whose Enrollment_NO is
23BCA04291. To do this, you have to write the following DML UPDATE command:

MariaDB [BPCCS]> UPDATE Section_I SET CGPA = 7.04 WHERE Enrollment_NO =


'23BCA04291';
Query OK, 1 row affected (0.239 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Example –2
This example describes how to update the value of multiple fields of the database table.
Suppose you want to update the CGPA of the student whose Enrollment_NO is
23BCA04291. To do this, you have to write the following DML UPDATE command:

MariaDB [BPCCS]> UPDATE Section_I SET CGPA = 5.8, Name_of_the_student = 'PATIL


ANJALI' WHERE Enrollment_NO = '23BCA04300';
Query OK, 1 row affected (0.080 sec)
Rows matched: 1 Changed: 1 Warnings: 0

Page 20 of 33
➢ Command-4:
DELETE DML Command:
▪ DELETE is a DML command which allows SQL users to remove single or
multiple existing records from the database tables.
▪ This command of Data Manipulation Language does not delete the stored data
permanently from the database.
▪ We use the WHERE clause with the DELETE command to select specific rows
from the table.
Syntax –
DELETE FROM Table_Name WHERE condition;

Example –
This example describes how to delete a single record from the table.
Suppose you want to delete that student from the Section_I table
whose Enrollment_NO is 23BCA04317.
To do this, you have to write the following DML DELETE command:

MariaDB [BPCCS]> DELETE FROM Section_I WHERE Enrollment_NO='23BCA04317';


Query OK, 1 row affected (0.074 sec)

Example – 2
Suppose you want to delete the record of those students whose CGPA is less than 6.00.
To do this, you have to write the following DML Update command:

MariaDB [BPCCS]> DELETE FROM Section_I WHERE CGPA<6.00;


Query OK, 1 row affected (0.036 sec)

Page 21 of 33
➢ Data Control Language (DCL):
▪ DCL is short for Data Control Language which acts as an access
specifier to the database (basically to grant and revoke
permissions to users in the database).

➢ Data Control Language (DCL) Commands:


GRANT: grant permissions to the user for running DML (SELECT, INSERT,
DELETE…) commands on the table
REVOKE: revoke permissions to the user for running DML(SELECT, INSERT,
DELETE,…) command on the specified table

Page 22 of 33
➢ Data Control Language (DCL) Commands Syntax:
Command-1:
▪ GRANT, as the name itself suggests, provides. This command
allows the administrator to provide particular privileges or
permissions over a database object, such as a table, view, or
procedure.
▪ It can provide user access to perform certain database or
component operations.
▪ In simple language, the GRANT command allows the user to
implement other SQL commands on the database or its objects.
▪ The primary function of the GRANT command in SQL is to
provide administrators the ability to ensure the security and
integrity of the data is maintained in the database.
▪ To have a better understanding of implementing the GRANT
statement in the database. Let us use an example.

Syntax –
GRANT SELECT ON your_database_name.your_table_name TO 'your_username'@'your_host';

Example – Implementing GRANT Statement Consider a scenario where you are the
database administrator, and a Section_I table is in the database.
Suppose you want a specific user VRAJ to only SELECT (read)/ retrieve the
data from the Section_I table. Then you can use GRANT in the below GRANT
statement.

GRANT SELECT ON BPCCS.Section_I TO 'PATEL VRAJ BHARATBHAI'@'localhost';


Query OK, 0 rows affected (0.685 sec)

Page 23 of 33
Command-2:
REVOKE Command:
▪ As the name suggests, revoke is to take away.
▪ The REVOKE command enables the database administrator to remove the
previously provided privileges or permissions from a user over a database or
database object, such as a table, view, or procedure.
▪ The REVOKE commands prevent the user from accessing or performing a specific
operation on an element in the database.
▪ In simple language, the REVOKE command terminates the ability of the user to
perform the mentioned SQL command in the REVOKE query on the database or
its component.
▪ The primary reason for implementing the REVOKE query in the database is to
ensure the data's security and integrity.
▪ Let us use an example to better understand how to implement the REVOKE
command in SQL.

Syntax –
REVOKE SELECT ON your_database_name.table_name FROM 'specific_user'@'localhost';

Example –
Consider a scenario where the user is the database administrator. In the above
implementation of the GRANT command, the user 'PATEL VRAJ BHARATBHAI' was provided
permission to implement a SELECT query on the Section_I table that allowed VRAJ to read or
retrieve the data from the table. Due to certain circumstances, the administrator wants to revoke
the abovementioned permission. To do so, the administrator can implement the below REVOKE
statement:
MariaDB [BPCCS]> REVOKE SELECT ON BPCCS.Section_I FROM 'PATEL VRAJ
BHARATBHAI'@'localhost';
Query OK, 0 rows affected (0.124 sec)

Page 24 of 33
➢ Transaction Control language (TCL):
▪ A single unit of work in a database is formed after the consecutive
execution of commands is known as a transaction.
▪ There are certain commands present in SQL known as TCL
commands that help the user manage the transactions that take place
in a database.
▪ ROLLBACK,COMMIT and SAVEPOINT are the most commonly
used TCL commands in SQL.
▪ Now let us take a deeper dive into the TCL commands of SQL with
the help of examples. All the queries in the examples will be written
using the MySQL database.

➢ Transaction Control Language (TCL) Commands:

Save Point: It is used to save the data on the temporary basis in the database.
Roll Back: Used to cancel or Undo changes made in the database.
Commit: It is used to apply or save changes in the database.

Page 25 of 33
➢ Transaction Control Language (TCL) Commands Syntax:
Command-1:
Save Point Command:
▪ We can divide the database operations into parts.
For example,
▪ we can consider all the insert related queries that we will execute
consecutively as one part of the transaction and the delete command
as the other part of the transaction.
▪ Using the SAVEPOINT command in SQL, we can save these
different parts of the same transaction using different names.
▪ For example, we can save all the insert related queries with the
savepoint named INS.
▪ To save all the insert related queries in one savepoint, we have to
execute the SAVEPOINT query followed by the savepoint name after
finishing the insert command execution.

Syntax –
START TRANSACTION;
SAVEPOINT savepoint_name;

Example –
MariaDB [BPCCS]> START TRANSACTION;
Query OK, 0 rows affected (0.000 sec)
MariaDB [BPCCS]> SAVEPOINT INS;
Query OK, 0 rows affected (0.046 sec)

Page 26 of 33
Command-2:
Roll Back Command:
▪ While carrying a transaction, we must create savepoints to save
different parts of the transaction.
▪ According to the user's changing requirements, he/she can roll back
the transaction to different savepoints.
▪ Consider a scenario: We have initiated a transaction followed by the
table creation and record insertion into the table.
▪ After inserting records, we have created a savepoint INS. Then we
executed a delete query, but later we thought that mistakenly we had
removed the useful record.
▪ Therefore in such situations, we have an option of rolling back our
transaction.
▪ In this case, we have to roll back our transaction using the
ROLLBACK command to the savepoint INS, which we have
created before executing the DELETE query.
Syntax –
ROLLBACK TO SAVEPOINT savepoint_name;
Example –
MariaDB [BPCCS]> ROLLBACK TO SAVEPOINT INS;
Query OK, 0 rows affected (0.000 sec)

Page 27 of 33
Command-3:
Commit Command:
▪ COMMIT command in SQL is used to save all the transaction-related
changes permanently to the disk.
▪ Whenever DDL commands such as INSERT, UPDATE and DELETE
are used, the changes made by these commands are permanent only
after closing the current session.
▪ So before closing the session, one can easily roll back the changes
made by the DDL commands.
▪ Hence, if we want the changes to be saved permanently to the disk
without closing the session, we will use the commit command.

Syntax –
COMMIT;
Example –
MariaDB [BPCCS]> COMMIT;
Query OK, 0 rows affected (0.086 sec)

Page 28 of 33
❖ Basic SQL Queries: SQL operators.

➢ Structured Query Language is a computer language that we


use to interact with a relational database.
➢ In simple operator can be defined as an entity used to perform
operations in a table.
➢ Operators are the foundation of any programming language.
➢ We can define operators as symbols that help us to perform
specific mathematical and logical computations on operands.
➢ In other words, we can say that an operator operates the
operands.
➢ SQL operators have five different categories.
➢ Types of SQL Operators:
▪ Arithmetic Operators.
▪ Bitwise Operators.
▪ Comparison Operators.
▪ Logical Operators.
▪ Compound Operators.

Page 29 of 33
➢ Arithmetic Operators:

➢ Bitwise Operators:

Page 30 of 33
➢ Comparison Operators:

➢ Logical Operators:

Page 31 of 33
➢ Compound Operators:

Page 32 of 33
❖ Basic SQL Queries: Creating Database, Creating, Modifying and
Deleting Tables, Queries: Insert, Select, Update, Delete, Where Clause.
[The answer to this question is shown in page 12 To 28]

➢ ORDER BY:
▪ The ORDER BY keyword is used to sort the result-set in
ascending or descending order.
Syntax –
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Example –
MariaDB [BPCCS]> SELECT * FROM Section_I ORDER BY Name_of_the_student;
2 rows in set (0.249 sec)
Example – 2
MariaDB [BPCCS]> SELECT * FROM Section_I ORDER BY Name_of_the_student DESC;
2 rows in set (0.249 sec)
▪ DESC:
▪ The ORDER BY keyword sorts the records in ascending order
by default. To sort the records in descending order, use the DESC
keyword.
Using Both ASC and DESC:
The following SQL statement selects all students from the "Section_I"
table, sorted ascending by the "Enrollment_NO" and descending by the
"Name_of_the_student" column:
Example –
SELECT * FROM Section_I
ORDER BY Enrollment_NO ASC, Name_of_the_student DESC;

X ---------------- X

Page 33 of 33

You might also like