Department of MIS
University of Dhaka
“A Report on Structured Query Language”
Course: Database Management
Course Code: MIS-401
Submitted To:
Dr. M. Helal Uddin Ahmed
Professor
Department of MIS
University of Dhaka
Submitted By:
Name: Farjana yeasmin
ID: 12-091
Section: A
Department of MIS, Faculty of Business Studies
University of Dhaka
Date of Submission: 1.06.2020
June 1, 2020
Dr. M. Helal Uddin Ahmed
Professor
Department of MIS
University of Dhaka
Subject: Submission of the report titled as ‘Structured Query Language’
Sir,
With great pleasure and honor submit the report prepared for the course
“Database Management”. It reflects the concept of structured query language
(SQL). I tried to accommodate as much information and relative issues as
possible. I would like to thank you for providing me with this opportunity.
Sincerely yours
Farjana yeasmin
ID: 12-091
BBA 12th Batch, Section: A
Department of Management Information Systems
Faculty of Business Studies
University of Dhaka
Table of Contents
Introduction: ................................................................................................................................... 1
Literature Review: .......................................................................................................................... 2
Data Analysis: .................................................................................................................................. 3
Findings: ........................................................................................................................................ 19
Conclusion: .................................................................................................................................... 19
Bibliography: ................................................................................................................................. 19
Introduction:
SQL stands for Structured Query language, pronounced as "S-Q-L" or sometimes
as "See-Quel". SQL is the standard language for dealing with Relational
Databases. SQL can be used to insert, search, update and delete database
records. SQL can do lots of other operations including optimizing and
maintenance of databases. Relational databases like MySQL Database, Oracle,
Ms SQL server, Sybase, etc uses SQL.
How to use SQL syntaxes?
SQL Example
SELECT * FROM Members WHERE Age > 30
SQL syntaxes used in these databases are almost similar, except the fact that
some are using few different syntaxes and even proprietary SQL syntaxes.
SQL may be a common language for the access and manipulation of databases.
SQL stands for structured query language. In keeping with ANSI (American
National Standards Institute), it's the quality language for relational database
management systems. SQL statements perform tasks like update information or
retrieve information from pre stored database. Some common database
management systems that use SQL are: Oracle, Microsoft SQL Server, Access
etc.
However, the quality SQL commands like "Select", "Insert", "Update", "Delete",
"Create", and "Drop" may be wont to accomplish virtually everything that one
must do with a information. It is used for keeping, manipulating and restoring
information that was hold on in a database. SQL is that the commonplace
language for electronic information service System. SQL includes a sub-language
for definition of schemas, the data definition language (DDL), along with the data
manipulation language (DML). Each of those have roots in early CODASYL
specifications. The third sub-language in SQL is employed for declaration of
queries, through the selection statement and relative joins. SQL permits users to
access information within the database management systems and permits to
embed within other SQL modules, libraries & pre-compilers. It additionally
permits users to hold on procedure, functions in a database and permits users to
line permissions on tables, procedures and views. Although SQL is an ISO
standard, there are different versions of the SQL language. However, they all
1
support at least the major commands (such as SELECT, UPDATE, DELETE,
INSERT, WHERE) in a similar manner.
The ease and presence of SQL have led many of the creators of the many non-
relational information stores to adopt subsets of SQL or return up with their own
SQL-like question languages.
Literature Review:
SQL is used to carry out many functions such as executing queries, updating and
altering records, creating views and setting permissions in a database. In SQL,
many important commands like select, alter database, drop table, drop index,
update, delete, create table, create database, create index, insert into, alter table
etc. are used. All the relational database management Systems (RDMS) like
MySQL, MS Access, Oracle, Sybase, Informix, and SQL Server use SQL as
standard database language.
Also, there are different dialects, like – Server using T-SQL, Oracle using
PL/SQL, MS Access version of SQL is named JET SQL (native format) etc. SQL
is employed to declare the data to return, and a SQL query processor and query
optimizer flip the SQL declaration into a query plan that is usually executed by
database engine. There are some other commands that are also used in SQL such
as where, order by, select top, select top percent, select count, sum and average,
join, union, group by, having, exists, any and all operator, Case, store procedure,
backup database, truncate, not null, unique, check, default, date etc. Different
programming codes are written and run on relational database management
systems.
To store and retrieve data from a database, web server should have access to a
database-system that uses the SQL language. If web server is hosted by an
Internet Service Provider (ISP), then one has to look for SQL hosting plans. The
most used SQL hosting databases are MS SQL Server, Oracle, MySQL, and MS
Access.
An SQL developer should decide what variety of information might be kept
within every column while making a table. The data type is a guideline for SQL
to understand what type of data is expected inside of each column, and it also
2
identifies how SQL will interact with the stored data. SQL defines the qualities
of the required set, not the strategy to extract a data set that conform to those
qualities. The application sends command over this connection. The DBMS
receive that message and sends it back. One can control this setting parameters
before issuing an SQL command, but this is an application-side option: DBMS
just says that data are available and provides commands to retrieve it.
Data Analysis:
A database contains one or numerous tables. Each of these tables is identified by
a name. The tables contain records with data. Semicolon is used to separate the
statements in database to execute more than one statement at the same call to SQL
server. SQL is not case sensitive so create is the same as CREATE.
SQL commands or statements, operators , constraints and data types are described
briefly in this section.
Select statement:
It is used for selecting data from a database. The syntax will be-
Select *from table_name;
For example- select *from EMPLOYEES;
For selecting columns- select age, city from EMPLOYEES;
The SQL SELECT DISTINCT Statement:
The SELECT DISTINCT statement is used to return only distinct (different)
values. The syntax will be-
SELECT DISTINCT
column1, column2
FROM table_name;
Example- select distinct city from EMPLOYEES;
The SQL WHERE Clause: The WHERE clause is used to filter records.
It is used to extract only those records that fulfill a specific condition. Syntax is-
SELECT column1, column2,
3
FROM table_name WHERE condition;
An example can be- SELECT * FROM Employee
WHERE Country='Mexico';
The SQL AND, OR and NOT Operators:
The AND and OR operators are used to filter records based on more than one
condition:
The AND operator shows a record if all the conditions separated by AND
are TRUE.
The OR operator shows a record if any of the conditions separated by OR
is TRUE.
The NOT operator displays a record if the condition is NOT TRUE.
Example of AND operator is-
SELECT * FROM STUDENT
WHERE Country='Germany' AND City='Berlin';
Example for OR operator is-
SELECT * FROM STUDENT
WHERE City='Berlin' OR City='Dhaka';
Example for NOT operator is-
SELECT * FROM STUDENT
WHERE NOT Country='Germany';
The operators can also be combined just like the following example-
SELECT * FROM STUDENT
WHERE Country='Germany' AND (City='Berlin' OR City='Dhaka');
The SQL ORDER BY Keyword:
The ORDER BY keyword is used to sort the result in ascending or descending
order. The syntax is-
SELECT column1, column2,
FROM table_name ORDER BY column1, column2, ASC|DESC;
The example of ORDER BY DESC is-
4
SELECT * FROM STUDENT
ORDER BY Country DESC;
For several columns-
SELECT * FROM STUDENT
ORDER BY Country, Name;
The SQL INSERT INTO Statement:
The INSERT INTO statement is used to insert new records in a database table.
Syntax will be-
INSERT INTO table_name
VALUES (value1, value2, value3);
Null Value:
Null value is a field with no value. It is impossible to test for NULL values with
comparison operators, such as =, <, or <>.
IS NULL Syntax-
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
IS NOT NULL Syntax:
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
The SQL UPDATE Statement:
The UPDATE statement is used for modification of the existing records in a table.
An example is given below-
UPDATE Student
SET ContactName='LILY'
WHERE Country='Mexico';
5
The SQL DELETE Statement:
The DELETE statement is used to delete existing records or all records without
deleting the table. The syntaxes are-
DELETE FROM table_name WHERE condition;
For deleting all records or rows-
DELETE FROM table_name;
The SQL SELECT TOP Clause:
The SELECT TOP clause is useful for large tables with many records. The
SELECT TOP clause is used for the specification of number of records to return.
Syntax for this one is-
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
Example of SQL top, limit, rownum is-
SELECT * FROM STUDENT
WHERE ROWNUM <= 3;
Or- SELECT TOP 50 PERCENT * FROM STUDENT;
The SQL MIN() and MAX() Functions:
The MIN() function returns the smallest value of selected column.
The MAX() function returns the largest value of selected column.
MIN() Syntax-
SELECT MIN(column_name)
FROM table_name
WHERE condition;
Example-
SELECT MIN(Price) AS SmallestPrice
6
FROM Product;
MAX() Syntax-
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Example-
SELECT MAX(Price) AS LargestPrice
FROM Product;
The SQL COUNT(), AVG() and SUM() Functions:
The COUNT() function returns the number of records which matches a specified
criterion. The AVG() function returns the average value of a numerical column.
The SUM() function returns the total sum of a numerical column.
COUNT() Syntax is-
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
AVG() Syntax is-
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SUM() Syntax is-
SELECT SUM(column_name)
FROM table_name
WHERE condition;
The SQL LIKE Operator:
The LIKE operator is used in a WHERE clause to find a specified pattern of a
column. There are two wildcards often used in conjunction with the LIKE
operator:
Example of % wildcard-
7
SELECT * FROM STUDENT WHERE City LIKE 'ber%';
Example of _ wildcard-
SELECT * FROM Customers WHERE City LIKE '_ondon';
The SQL IN Operator:
The IN operator is a shorthand for multiple OR conditions.
Example of in operator-
SELECT * FROM Customer
WHERE City IN (SELECT City FROM Suppliers);
The SQL BETWEEN Operator :
The BETWEEN operator is inclusive: begin and end values both are included in
this operator.
Syntax for this one is-
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Example-
SELECT * FROM Product
WHERE Price BETWEEN 10 AND 20;
Example of NOT BETWEEN-
SELECT * FROM Product
WHERE Price NOT BETWEEN 10 AND 20;
Example of BETWEEN WITH IN-
SELECT * FROM Product
WHERE Price BETWEEN 10 AND 20
AND ProductID NOT IN (1, 2, 3);
Example of BETWEEN DATES-
SELECT * FROM Product WHERE OrderDate BETWEEN #01/06/1996# AND
#31/06/1996#;
Example of BETWEEN TEXT values-
8
SELECT * FROM Product
WHERE ProductName BETWEEN 'Carnarvon Tigers' AND 'Mozzarella di
Giovanni' ORDER BY ProductName;
Example of NOT BETWEEN TEXT values-
SELECT * FROM Product
WHERE ProductName NOT BETWEEN 'Carnarvon Tigers' AND 'Mozzarella
di Giovanni' ORDER BY ProductName;
SQL Aliases:
Aliases are used to make column names more readable. An alias exists for the
duration of query.
Alias Column Syntax is-
SELECT column_name AS alias_name
FROM table_name;
Alias Table Syntax is-
SELECT column_name(s)
FROM table_name AS alias_name;
Aliases can be useful when:
• There are more than one table involved
• Functions are used in database
• Column names are not very readable
• Two or more columns are combined
The SQL GROUP BY Statement:
The GROUP BY statement groups rows which consists of the same values into
summary rows.
Syntax will be-
SELECT column_name
FROM table_name
WHERE condition
9
GROUP BY column_is-name
ORDER BY column_name;
Example-
SELECT COUNT(CustomerID), Country
FROM Student
GROUP BY Country;
The SQL HAVING Clause:
This clause is used instead of where clause with aggregate functions. The syntax
is- SELECT column_name
FROM table_name
WHERE condition
GROUP BY column_name
HAVING condition
ORDER BY column_name;
Example-
SELECT COUNT StudentID,Country
FROM Students
GROUP BY Country
HAVING COUNT(StudentID) > 5;
The SQL UNION Operator:
The UNION operator is used to combine the result of two or more SELECT
statements.
Syntax is-
SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;
SQL JOIN:
10
This clause is used to combine rows from two or more than two tables, based on
a related column between them.
Example-
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
SQL LEFT JOIN :
The syntax is-
SELECT column_name
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Example-
SELECT Customer.CustomerName, Order.OrderID
FROM Customer
LEFT JOIN Orders ON Customer.CustomerID = Order.CustomerID
ORDER BY Customer.CustomerName;
SQL RIGHT JOIN:
Syntax-
SELECT column_name(s)
FROM table1
RIGHT JOIN table3
ON table1.column_name = table3.column_name;
Example-
SELECT Order.OrderID, Employee. Name
FROM Order
RIGHT JOIN Employee ON Order.EmployeeID = Employee.EmployeeID
ORDER BY Order.OrderID;
SQL FULL OUTER JOIN example:
11
SELECT Customer.CustomerName, Order.OrderID
FROM Customer
FULL OUTER JOIN Order ON Customer.CustomerID=Order.CustomerID
ORDER BY Customer.CustomerName;
SQL SELF JOIN Example-
SELECT B.CustomerName AS CustomerName1, C.CustomerName AS
CustomerName2, B.City
FROM Customers B, Customers C
WHERE B.CustomerID <>C.CustomerID
AND B.City =C.City
ORDER BY B.City;
The SQL EXISTS Operator:
The EXISTS operator returns true if the sub-query returns records.
Example-
SELECT SupplierName
FROM Supplier
WHERE EXISTS (SELECT ProductName FROM Product WHERE
Product.SupplierID = Supplier.supplierID AND Price < 20);
The SQL ANY and ALL Operators:
The ANY operator returns true if any of the sub-query values meet the condition.
The ALL operator returns true if all of the sub-query values meet the condition.
Syntax for ANY operator-
SELECT column_name FROM table_name
WHERE column_name operator ANY
(SELECT column_name FROM table_name WHERE condition);
Syntax for ALL operator-
SELECT column_name
12
FROM table_name
WHERE column_name operator ALL
(SELECT column_name FROM table_name WHERE condition);
The SQL SELECT INTO Statement:
This Statement copies data from one table to another table.
Example-
SELECT * INTO StudentsBackup2017
FROM Students;
The SQL INSERT INTO SELECT Statement:
This statement copies data from one table and then inserts that data into another
table.
Example-
INSERT INTO Customer (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Supplier;
The SQL CASE Statement:
The CASE statement goes through conditions and returns a value if the first
condition is met. So, if a condition is true, it will stop reading and return the result.
If there is no true condition, it returns the value in the ELSE clause.
The syntax is-
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
SQL IFNULL(), ISNULL(), COALESCE(), and NVL() Functions:
13
SELECT ProductName, UnitPrice * (UnitsInStock + NVL(UnitsOrdered, 0))
FROM Product;
SQL Stored Procedures for SQL Server:
A stored procedure is a prepared SQL code that can be saved, so the code can be
reused over and over again. The syntax is-
CREATE PROCEDURE procedure_name
AS
sql_statement
GO;
Executing stored procedure syntax-
EXEC procedure_name;
SQL Comments:
Comments are used to explain sections or to prevent execution of SQL
statements.
Example:
--SELECT * FROM Customer;
SELECT * FROM Product;
The SQL CREATE DATABASE Statement:
The CREATE DATABASE statement is used to create a new database.
Example-
CREATE DATABASE Student;
The SQL DROP DATABASE Statement:
The DROP DATABASE statement is used to drop a database.
Example-
Drop database Student;
14
The SQL BACKUP DATABASE Statement:
The BACKUP DATABASE statement is used to create a full back up of an
existing database. The syntax is-
BACKUP DATABASE databasename
TO DISK = 'filepath';
The SQL BACKUP WITH DIFFERENTIAL Statement:
A differential backup only backs up the parts that have been changed since last
full backup.
The syntax is-
BACKUP DATABASE databasename
TO DISK = 'filepath'
WITH DIFFERENTIAL;
The SQL CREATE TABLE Statement:
CREATE TABLE STUDENTS
(
StuID int,
Address varchar(25),
City varchar(25)
);
The SQL DROP TABLE Statement:
The DROP TABLE statement is used to drop a table in a database.
The syntax is-
DROP TABLE table_name;
SQL TRUNCATE TABLE:
TRUNCATE TABLE table_name;
15
SQL ALTER TABLE Statement:
ALTER TABLE: ADD Column-
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE: DROP COLUMN-
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE: ALTER/MODIFY COLUMN-
ALTER TABLE table_name
MODIFY column_name datatype;
SQL Constraints:
Constraints are used to limit the type of data that is used in a table. This ensures
the accuracy of the data in table. Commonly used constraints are NOT NULL,
UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, INDEX etc.
SQL NOT NULL Constraint- It enforces a column to not accept null values.
Example-
CREATE TABLE Students
( ID int NOT NULL,
Name varchar(25) NOT NULL,
Age int);
SQL UNIQUE Constraint- It ensures that all values in a column are unique or
different.
Example-
CREATE TABLE Students
( ID int NOT NULL,
Name varchar(25) NOT NULL,
Age int
UNIQUE (ID));
16
SQL PRIMARY KEY Constraint: Primary keys must have unique values and
cannot have null values.
Example-
CREATE TABLE Students
( ID int NOT NULL PRIMARY KEY,
Name varchar(25) NOT NULL,
Age int);
SQL FOREIGN KEY Constraint: Foreign key is a field in one table that refers to
primary key of another table.
Example-
CREATE TABLE Orders (
OrderID int NOT NULL PRIMARY KEY,
OrderNumber int,
PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
);
SQL CHECK Constraint: It is used to limit value range that can be in a column.
Example-
CREATE TABLE Students
( ID int NOT NULL,
Name varchar(25) NOT NULL,
Age int CHECK (Age>=18));
SQL DEFAULT Constraint: It is used to provide default value in a column.
Example-
CREATE TABLE Students
( ID int NOT NULL,
Name varchar(25) NOT NULL,
Age int,
City varchar(25) DEFAULT 'Dhaka');
SQL CREATE INDEX Statement:
17
It is used to create indexes in tables.
Example-
CREATE INDEX idx_pname
ON Students (LastName, FirstName);
DROP INDEX Statement:
The syntax is-
DROP INDEX index_name;
AUTO INCREMENT Field:
Auto-increment allows to generate a unique number automatically when a new
record is inserted into a table.
Example-
CREATE SEQUENCE seq_student
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;
SQL Date Data Types:
• DATE – format: YYYY-MM-DD
• DATETIME - format: YYYY-MM-DD HH: MI: SS
• TIMESTAMP - format: YYYY-MM-DD HH: MI: SS
• YEAR – format: YYYY or YY
SQL CREATE VIEW Statement: The syntax for this one is-
CREATE VIEW view_name AS
SELECT column1, column2,
FROM table_name
WHERE condition;
18
SQL Injection:
SQL injection is a code injection technique that can destroy a database by placing
malicious SQL statements through web page input.
Example-
txtUserId = getRequestString("UserId");
txtSQL = "SELECT * FROM Users WHERE UserId = " + txtUserId;
SQL Data Type:
There are three main data types: string, numeric, and date and time. Some string
data types are CHAR, VARCHAR, VARBINARY, TINYBLOB, TINYTEXT,
MEDIUMTEXT, LONGTEXT, ENUM, SET etc. Some numeric data types are
BIT, TINYINT, INT, BOOL, BOOLEAN, SMALLINT,MEDIUMIY,BIGINT,
INTEGER, FLOAT, DOUBLE, DOUBLE PRECISION, DECIMAL, DEC etc.
Some date and time data types are DATE, TIMESTAMP, DATETIME, TIME,
YEAR etc.
Findings:
So from this report, it can be found that SQL is a language that is used to
get access and to manipulate database. The basis of SQL is RDMS which is used
in relational database systems like MYSQL, IBM DB2, SQL Server, Oracle etc.
there are different types of functions that are used in SQL. There are many
statements like create, join, backup, delete, drop, aliases, stored procedure etc.
Comments are added in databases by using SQL comment. There are also
different types of data types for SQL. String, Time and Date, numeric etc. are the
data types that are used in SQL.
Conclusion:
Database management has always been a complicated process but SQL has
rationalized it to some extent. Clauses, queries, statements etc. are some of the
elements of SQL that help to create and manage database. Although the language
is often modified by the vendors, but SQL has become the standard language for
relational database communication by ANSI approval.
Bibliography:
https://www.w3schools.com/sql/default.asp
https://www.w3schools.com/sql/sql_datatypes.asp
19
20