SQL (Structured Query Language) Is Used To Perform Operations On The Records Stored in The Database

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

SQL

SQL (Structured Query Language) is used to perform operations on the records stored in the database
such as updating records, deleting records, creating and modifying tables, views, etc.

SQL is just a query language; it is not a database. To perform SQL queries, you need to install any
database, for example, Oracle, MySQL, MongoDB, PostGre SQL, SQL Server, DB2, etc.

What is SQL?

o SQL stands for Structured Query Language.


o It is designed for managing data in a relational database management system (RDBMS).
o SQL is a database language, it is used for database creation, deletion, fetching rows, and
modifying rows, etc.

Why is SQL required?

SQL is required:

o To create new databases, tables and views


o To insert records in a database
o To update records in a database
o To delete records from a database
o To retrieve data from a database

What SQL does

o With SQL, we can query our database in several ways, using English-like statements.
o With SQL, a user can access data from a relational database management system.
o It allows the user to describe the data.
o It allows the user to define the data in the database and manipulate it when needed.
o It allows the user to create and drop database and table.
o It allows the user to create a view, stored procedure, function in a database.
o It allows the user to set permission on tables, procedures, and views.

Syntax:

SELECT "column_name" FROM "table_name"; 

Commands:

DDL (Data Definition Language)- CREATE, ALTER, DROP, RENAME, TRUNCATE

DML (Data Manipulation Language)- SELECT, INSERT, UPDATE, DELETE


DCL (Data Control Language)- GRANT, REVOKE

TCL (Transaction Control Language)- COMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION

These are the some important SQL commands:

o SELECT: it extracts data from a database.


o UPDATE: it updates data in database.
o DELETE: it deletes data from database.
o CREATE TABLE: it creates a new table.
o ALTER TABLE: it is used to modify the table.
o DROP TABLE: it deletes a table.
o CREATE DATABASE: it creates a new database.
o ALTER DATABASE: It is used to modify a database.
o INSERT INTO: it inserts new data into a database.
o CREATE INDEX: it is used to create an index (search key).
o DROP INDEX: it deletes an index.

1. CREATE:

CREATE DATABASE database_name;

2. DROP

SQL DROP statement is used to delete or remove indexes from a table in the database.

If you want to delete or drop an existing database in a SQL schema, you can use SQL DROP
DATABASE

DROP DATABASE database_name; 

3. RENAME

SQL RENAME DATABASE is used when you need to change the name of your database. Sometimes it
is used because you think that the original name is not more relevant to the database or you want
to give a temporary name to that database.

RENAME DATABASE old_db_name TO new_db_name;

4. SELECT Database

In MySQL database, you need to select a database first before executing any query on table, view
etc.

USE  database_name;
Table:

Table is a collection of data, organized in terms of rows and columns. In DBMS terms, table is known
as relation and row as tuple. Table is the simple form of data storage. A table is also considered as a
convenient representation of relations.

A table can have specified number of columns, but can have any number of rows.

Employee

EMP_NAME ADDRESS EMAIL

Ankit Lucknow [email protected]


Raman Allahabad [email protected]
Mike New York [email protected]

Salary

EMP_NAME TAXES SALARY

Ankit 1000 15000


Raman 1500 18000
Mike 2000 20000

Here, "Employee" is the table name, "EMP_NAME", "ADDRESS" and "SALARY" are the column names.
The combination of data of multiple columns forms a row e.g. "Ankit", "Lucknow" and 15000 are the
data of one row.

1. SQL CREATE TABLE

If you want to create a table, you should name the table and define its column and each column's data
type.

Syntax: create table "tablename"  
("column1" "data type",  
"column2" "data type",  
"column3" "data type",  
...  
"columnN" "data type");

Ex: CREATE TABLE Employee
(  
EmployeeID int,  
FirstName varchar(255),
LastName varchar(255),  
Email varchar(255),  
AddressLine varchar(255),  
City varchar(255)  
);

Create a Table using another table

We can create a copy of an existing table using the create table command. The new table gets the same
column signature as the old table. We can select all columns or some specific columns.

If we create a new table using an old table, the new table will be filled with the existing value from the
old table.

CREATE TABLE EmployeeCopy AS  
SELECT EmployeeID, FirstName, Email  
FROM Employee;

SQL Primary Key with CREATE TABLE Statement

The following query creates a PRIMARY KEY on the "D" column when the "Employee" table is created.
CREATE TABLE Employee(  
EmployeeID int NOT NULL,  
FirstName varchar(255) NOT NULL,  
LastName varchar(255),  
City varchar(255),  
PRIMARY KEY (EmployeeID)  
);

Use the following query to define a PRIMARY KEY constraints on multiple columns, and to allow naming
of a PRIMARY KEY constraints.
CREATE TABLE Employee(  
EmployeeID NOT NULL,  
FirstName varchar(255) NOT NULL,  
LastName varchar(255),  
City varchar(255),  
CONSTRAINT     PK_Employee PRIMARY KEY (EmployeeID, FirstName)  
);  

SQL DROP TABLE

A SQL DROP TABLE statement is used to delete a table definition and all data from a table.
This is very important to know that once a table is deleted all the information available in the table is
lost forever, so we have to be very careful when using this command.

DROP TABLE "table_name"; 

SQL DELETE TABLE

The DELETE statement is used to delete rows from a table. If you want to remove a specific row from a
table you should use WHERE condition.

DELETE FROM table_name [WHERE condition]; 

But if you do not specify the WHERE condition it will remove all the rows from the table.
DELETE FROM table_name;

Difference between DELETE and TRUNCATE statements

There is a slight difference b/w delete and truncate statement. The DELETE statement only deletes the
rows from the table based on the condition defined by WHERE clause or delete all the rows from the
table when condition is not specified.

But it does not free the space containing by the table.

The TRUNCATE statement: it is used to delete all the rows from the table and free the containing space.

TRUNCATE TABLE employee;

Difference b/w DROP and TRUNCATE statements

When you use the drop statement it deletes the table's row together with the table's definition so all
the relationships of that table with other tables will no longer be valid.

When you drop a table:

o Table structure will be dropped


o Relationship will be dropped
o Integrity constraints will be dropped
o Access privileges will also be dropped

On the other hand when we TRUNCATE a table, the table structure remains the same, so you will not
face any of the above problems.

SQL TRUNCATE TABLE


A truncate SQL statement is used to remove all rows (complete data) from a table. It is similar to the
DELETE statement with no WHERE clause.
TRUNCATE TABLE table_name; 
SQL RENAME TABLE

SQL RENAME TABLE syntax is used to change the name of a table. If we choose a non-meaningful name
for the table, it is required to be changed.

ALTER TABLE table_name   
RENAME TO new_table_name;

Ex: ALTER TABLE STUDENTS  
RENAME TO ARTISTS;

SQL ALTER TABLE

The ALTER TABLE statement is used to add, modify or delete columns in an existing table. It is also used
to rename a table.

You can also use SQL ALTER TABLE command to add and drop various constraints on an existing table.

If you want to add columns in SQL table, the SQL alter table syntax is:

ALTER TABLE table_name ADD column_name column-definition;

If you want to add multiple columns in table, the SQL table will be:

ALTER TABLE table_name   
ADD (column_1 column-definition,  
           column_2 column-definition,  
           .....  
           column_n column-definition); 

Ex: alter table info


add Mobile int NOT NULL
after City;
OR
alter table info
add Address varchar(255) NOT NULL;

DML Statements:

-SELECT statement:

The most commonly used SQL command is SELECT statement. It is used to query the database and
retrieve selected data that follow the conditions we want.
In simple words, we can say that the select statement used to query or retrieve data from a table in the
database.

SELECT expressions  
FROM tables  
WHERE conditions;

There are some optional clauses in SELECT statement:

[WHERE Clause] : It specifies which rows to retrieve.

[GROUP BY Clause] : Groups rows that share a property so that the aggregate function can be applied to
each group.

[HAVING Clause] : It selects among the groups defined by the GROUP BY clause.

[ORDER BY Clause] : It specifies an order in which to return the rows.

Ex:

ID First_name Last_name Age Subject Hobby

1 Amar Sharma 20 Math Cricket


2 Akbar Khan 22 Biology Football
3 Anthony Milton 25 Commerce Gambling

SELECT first_name FROM student_details;

select * from student_details;

INSERT Statement:

SQL INSERT statement is a SQL query. It is used to insert a single or a multiple records in a table.

Syntax:

INSERT INTO table_name  
VALUES (value1, value2, value3....);

Ex:
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)  
VALUES (1, ‘ABHIRAM’, 22, ‘ALLAHABAD’); 

UPDATE Statement:
The SQL commands (UPDATE and DELETE) are used to modify the data that is already in the database.
The SQL DELETE command uses a WHERE clause.
SQL UPDATE statement is used to change the data of the records held by tables. The rows that need to
be updated are decided by a condition. To specify the condition, we use WHERE clause.

Syntax:
UPDATE table_name  
SET column_name = expression  
WHERE conditions

////SET SQL_SAFE_UPDATES = 0;

update info set Lastname=xyz where EmployeeID= '2';

‘WHERE’ Clause:

A WHERE clause in SQL is a data manipulation language statement.

WHERE clauses are not mandatory clauses of SQL DML statements. But it can be used to limit the
number of rows affected by a SQL DML statement or returned by a query.

It filters the records. It returns only those queries which fulfill the specific conditions. WHERE clause is
used in SELECT, UPDATE, DELETE statement etc.

SELECT column1, column 2, ... column n
FROM    table_name  
WHERE [conditions]

SQL AND, OR:

The SQL AND condition is used in SQL query to create two or more conditions to be met. It is used in SQL
SELECT, INSERT, UPDATE and DELETE statements.

SELECT columns  
FROM tables  
WHERE condition 1  
AND condition 2;

Ex: SQL “AND” example with “UPDATE” statement


This is how the "AND" condition can be used in the SQL UPDATE statement.
UPDATE suppliers  
SET supplier_name = 'HP'  
WHERE supplier_name = 'IBM'  
AND offices = 8;

SELECT DISTINCT:
The SQL DISTINCT command is used with SELECT key word to retrieve only distinct or unique data.

In a table, there may be a chance to exist a duplicate value and sometimes we want to retrieve only
unique values. In such scenarios, SQL SELECT DISTINCT statement is used.

Syntax:

SELECT DISTINCT column_name ,column_name  
FROM  table_name;

Ex:

Student_Name Gender Mobile_Number HOME_TOWN

Rahul Ojha Male 7503896532 Lucknow


Disha Rai Female 9270568893 Varanasi
Sonoo Jaiswal Male 9990449935 Lucknow

Here is a table of students from where we want to retrieve distinct information For example: distinct
home-town.

SELECT DISTINCT home_town  
FROM students

HOME_TOW
N

Lucknow

Varanasi

SQL Logical Operators:


Select Count:

The SQL COUNT() function is used to return the number of rows in a query. The COUNT() function is
used with SQL SELECT statement and it is very useful to count the number of rows in a table having
enormous data.

SELECT COUNT(*) FROM employee_table;

SELECT COUNT(ProductID)
FROM Products;

AVG():

The AVG() function returns the average value of a numeric column.

Ex: SELECT AVG(Price)
FROM Products;

MIN() & MAX():

The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.


Ex: SELECT MIN(Price) AS SmallestPrice
FROM Products;

SQL LIKE Operator:

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:

  The percent sign (%) represents zero, one, or multiple characters


  The underscore sign (_) represents one, single character

Ex: SELECT * FROM Customers
WHERE CustomerName LIKE 'a%'; // Alex, Abraham, Alekhya, Andy- Alex, Alekhya

Ex: SELECT * FROM Customers
WHERE CustomerName LIKE '%a'; // ‘a%i’, ‘a_ex’, ‘%fany’

ALIAS:

SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to
make column names more readable.

An alias only exists for the duration of that query. An alias is created with the AS keyword.

SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;

Select Top:

The SQL SELECT TOP Statement is used to select top data from a table. The top clause specifies that how
many rows are returned.

Ex: SELECT TOP 2 * FROM employee

ORDER BY Clause:

The SQL ORDER BY clause is used for sorting data in ascending and descending order based on one or
more columns.

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.

Syntax:
SELECT column1,  column2,...
FROM table_name
ORDER BY column1, column2, ...  ASC|DESC;

Ex: SELECT * FROM Customers
ORDER BY Country;

DESC Ex: SELECT * FROM Customers


ORDER BY Country DESC;

ORDER BY several columns:

The following SQL statement selects all customers from the "Customers" table, sorted by the "Country"
and the "CustomerName" column. This means that it orders by Country, but if some rows have the same
Country, it orders them by CustomerName.

Ex: SELECT * FROM Customers
ORDER BY Country, CustomerName;

SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;

GROUP BY Statement:

The GROUP BY statement groups rows that have the same values into summary rows, like "find the
number of customers in each country".

The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG())


to group the result-set by one or more columns.

Syntax: SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

Ex: SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country;

Ex:

Orders Table
OrderID CustomerID EmployeeID OrderDate ShipperID

10248 90 5 1996-07-04 3

10249 81 6 1996-07-05 1

10250 34 4 1996-07-08 2

Shippers Table:

ShipperID ShipperName

1 Speedy Express

2 United Package

3 Federal Shipping

SELECT Shippers.ShipperName,COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders

LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID

GROUP BY ShipperName;

Output:

Number of Records: 3

ShipperName NumberOfOrders

Federal Shipping 68

Speedy Express 54

United Package 74

The following SQL statement lists the number of customers in each country, sorted high to low:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

HAVING:

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate
functions.

Syntax: SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

Ex: The following SQL statement lists the number of customers in each country. Only include countries
with more than 5 customers

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

The following SQL statement lists the number of customers in each country, sorted high to low (Only
include countries with more than 5 customers).

SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

DATA TYPES:

Char, varchar, binary, text, enum(val1, val2, val3…)

Numeric Data Types: bit, boolean, smallint, int, float, double, decimal.

MySQL comes with the following data types for storing a date or a date/time value in the database:

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

The SELECT DISTINCT statement is used to return only distinct (different) values.

Inside a table, a column often contains many duplicate values; and sometimes you only want to list the
different (distinct) values.

Syntax: SELECT DISTINCT column1,  column2, ...


FROM table_name;

Ex: SELECT DISTINCT Country FROM Customers;

SELECT COUNT(DISTINCT Country) FROM Customers;

SQL JOINS:

A JOIN clause is used to combine rows from two or more tables, based on a related column between
them.

Orders table:

OrderID CustomerID OrderDate

10308 2 1996-09-18

10309 37 1996-09-19

10310 77 1996-09-20

Customers table:

CustomerI CustomerName ContactName Country


D

1 Alfreds Futterkiste Maria Anders Germany

2 Ana Trujillo Emparedados y Ana Trujillo Mexico


helados
3 Antonio Moreno Taquería Antonio Mexico
Moreno

Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the
"Customers" table. The relationship between the two tables above is the "CustomerID" column.

Then, we can create the following SQL statement (that contains an INNER JOIN), that selects records
that have matching values in both tables.

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate


FROM Orders INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

Result:

OrderID CustomerName OrderDate

10308 Ana Trujillo Emparedados y helados 9/18/1996

10365 Antonio Moreno Taquería 11/27/1996

10383 Around the Horn 12/16/1996

10355 Around the Horn 11/15/1996

10278 Berglunds snabbköp 8/12/1996

Different Types of SQL JOINs

Here are the different types of the JOINs in SQL:

 (INNER) JOIN: Returns records that have matching values in both tables
 LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the
right table
 RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the
left table
 FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
       

SQL INNER JOIN:


The INNER JOIN keyword selects records that have matching values in both tables. The INNER
JOIN keyword selects all rows from both tables as long as there is a match between the columns.
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name  =  table2.column_name;

JOIN Three Tables:

SELECT Orders.OrderID, Customers.CustomerName, Shippers.ShipperName


FROM ((Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);

SQL LEFT JOIN:


The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from
the right table (table2). The result is 0 records from the right side, if there is no match.
The LEFT JOIN keyword returns all records from the left table (Customers), even if there are no matches
in the right table (Orders).

Syntax: SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name  =  table2.column_name;

Ex:
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;

SQL RIGHT JOIN:


The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records from
the left table (table1). The result is 0 records from the left side, if there is no match.
Syntax: SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name  =  table2.column_name;

SELECT Orders.OrderID, Employees.LastName, Employees.FirstName


FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;

SQL FULL OUTER JOIN:

The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2)
table records.

Ex: SELECT Customers.CustomerName, Orders.OrderID


FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

PRIMARY KEY:

The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must contain
UNIQUE values, and cannot contain NULL values.

A table can have only ONE primary key; and in the table, this primary key can consist of single or
multiple columns (fields).

Ex: CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    PRIMARY KEY (ID)
);

To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple
columns, use the following SQL syntax:

Ex: CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
);
To create a PRIMARY KEY constraint on the "ID" column when the table is already created, use the
following:

ALTER TABLE Persons
ADD PRIMARY KEY (ID);

ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);

If you use ALTER TABLE to add a primary key, the primary key column(s) must have been declared to not
contain NULL values (when the table was first created).

DROP A PRIMARY KEY Constraint:

ALTER TABLE Persons
DROP PRIMARY KEY;

FOREIGN KEY:

A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another
table. The table with the foreign key is called the child table and the table with the primary key is called
the referenced or parent table.

The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column,
because it has to be one of the values contained in the parent table.

SQL FOREIGN KEY on CREATE TABLE:

Ex: CREATE TABLE Orders (
    OrderID int NOT NULL UNIQUE,
    OrderNumber int NOT NULL UNIQUE,
    PersonID int UNIQUE,
    PRIMARY KEY (OrderID),
    FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY constraint on multiple
columns, use the following :

Ex: CREATE TABLE Orders (
    OrderID int NOT NULL,
    OrderNumber int NOT NULL,
    PersonID int,
    PRIMARY KEY (OrderID),
    CONSTRAINT FK_PersonOrder 
FOREIGN KEY (PersonID, Email)   REFERENCES Persons(PersonID)
);

SQL UNIQUE Constraint:

The UNIQUE constraint ensures that all values in a column are different. Both the UNIQUE and PRIMARY
KEY constraints provide a guarantee for uniqueness for a column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint.

However, you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per
table.

Ex: CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    UNIQUE (ID)
);

SQL NOT NULL:

By default, a column can hold NULL values. The NOT NULL constraint enforces a column to NOT accept
NULL values.

Ex: CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255) NOT NULL,
    Age int
);

SQL SUBQUERY:

In SQL a Subquery can be simply defined as a query within another query. In other words we can say
that a Subquery is a query that is embedded in WHERE clause of another SQL query.
Important rules for Subqueries:
 You can place the Subquery in a number of SQL clauses: WHERE clause, HAVING clause, FROM
clause.
Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements along with expression
operator. It could be equality operator or comparison operator such as =, >, =, <= and Like operator.
 A subquery is a query within another query. The outer query is called as main query and inner
query is called as subquery.
 The subquery generally executes first, and its output is used to complete the query condition for
the main or outer query.
 Subquery must be enclosed in parentheses.
 Subqueries are on the right side of the comparison operator.
 ORDER BY command cannot be used in a Subquery. GROUPBY command can be used to perform
same function as ORDER BY command.
 Use single-row operators with singlerow Subqueries. Use multiple-row operators with multiple-
row Subqueries.

Subqueries are seen to be used most frequently with SELECT statement as below:

Syntax: SELECT column_name


FROM table_name
WHERE column_name expression operator
( SELECT COLUMN_NAME from TABLE_NAME WHERE ... );

Ex: DATABASE Table

NAME ROLL_NOLOCATION PHONE_NUMBER

Ram 101 Chennai 9988775566

Raj 102 Coimbatore 8877665544

Sasi 103 Madurai 7766553344

Ravi 104 Salem 8989898989

Sumath
i 105 Kanchipuram 8989856868

STUDENT Table

NAME ROLL_NOSECTION

Ravi 104 A
Sumath
i 105 B

Raj 102 A

To display NAME, LOCATION, PHONE_NUMBER of the students from DATABASE table whose section is
A,

Select NAME, LOCATION, PHONE_NUMBER from DATABASE


WHERE ROLL_NO IN
(SELECT ROLL_NO from STUDENT where SECTION=’A’);

First subquery executes “ SELECT ROLL_NO from STUDENT where SECTION=’A’ ” and returns ROLL_NO
from STUDENT table whose SECTION is ‘A’. Then outer-query executes it and returns the NAME,
LOCATION, PHONE_NUMBER from the DATABASE table of the student whose ROLL_NO is returned from
inner subquery.

Output:

NAM
E ROLL_NO LOCATION PHONE_NUMBER

Ravi 104 Salem 8989898989

Raj 102 Coimbatore 8877665544

RANK():

The ranking functions in MySql are used to rank each row of a partition. The ranking functions are also
part of MySQL windows functions list.
 These functions are always used with OVER() clause.
 The ranking functions always assign rank on basis of ORDER BY clause.
 The rank is assigned to rows in a sequential manner.
 The assignment of rank to rows always start with 1 for every new partition.

There are 3 types of ranking functions supported in MySQL-


1. dense_rank():
This function will assign rank to each row within a partition without gaps. Basically, the ranks are
assigned in a consecutive manner i.e if there is a tie between values then they will be assigned the
same rank, and next rank value will be one greater than the previous rank assigned.
2. rank():
This function will assign rank to each row within a partition with gaps. Here, ranks are assigned in a
non-consecutive manner i.e if there is a tie between values then they will be assigned same rank, and
next rank value will be previous rank + no of peers (duplicates).
3. percent_rank():
It returns the percentile rank of a row within a partition that ranges from 0 to 1. It tells the percentage
of partition values less than the value in the current row, excluding the highest value.

Table: result

s_name subjects mark

Pratibha Maths 100

Ankita Science 80

Swarna English 100

Ankita Maths 65

Pratibha Science 80

Swarna Science 50

Pratibha English 70

Swarna Maths 85

Ankita English 90

1. dense_rank() function-

SELECT subjects, s_name, mark, dense_rank()


OVER ( partition by subjects order by mark desc )
AS 'dense_rank' FROM result;
Output:

Subject Mar Dense_ran


s Name k k

English Swarna 100 1

English Ankita 90 2

Pratibh
English a 70 3

Pratibh
Maths a 100 1
Subject Mar Dense_ran
s Name k k

Maths Swarna 85 2

Maths Ankita 65 3

Science Ankita 80 1

Pratibh
Science a 80 1

Science Swarna 50 2

Here, table is partitioned on the basis of “subjects”.


order by clause is used to arrange rows of each partition in descending order by “mark”.
dense_rank() is used to rank students in each subject.

2. rank() function-
SELECT subjects, s_name, mark, rank()
OVER ( partition by subjects order by mark desc )
AS 'rank' FROM result;
Output:

Subjects Name Mark rank

English Swarna 100 1

English Ankita 90 2

English Pratibha 70 3

Maths Pratibha 100 1

Maths Swarna 85 2

Maths Ankita 65 3

Science Ankita 80 1

Science Pratibha 80 1

Science Swarna 50 3
previous rank + no of peers (duplicates)
Explanation-
It’s output is similar to dense_rank() function.
Except, that for Science subject in case of a tie between Ankita and Pratibha, the next rank value is
incremented by 2 i.e 3 for Swarna.

3. percent_rank() function-
SELECT subjects, s_name, mark, percent_rank()
OVER ( partition by subjects order by mark )
AS 'percent_rank' FROM result;
Output:

Subjects Name Mark percent_rank

English Pratibha 70 0

English Ankita 90 0.5

English Swarna 100 1

Maths Ankita 65 0

Maths Swarna 85 0.5

Maths Pratibha 100 1

Science Swarna 50 0

Science Pratibha 80 0.5

Science Ankita 80 0.5


Here, the percent_rank() function calculate percentile rank in ascending order by “mark” column.
percent_rank is calculated using following formula-
(rank - 1) / (rows - 1)
rank is the rank of each row of the partition resulted using rank() function.
rows represent the no of rows in that partition.
To, clear this formula consider following query-
SELECT subjects, s_name, mark, rank()
OVER ( partition by subjects order by mark )-1
AS 'rank-1', count(*) over (partition by subjects)-1
AS 'total_rows-1', percent_rank()
OVER ( partition by subjects order by mark ) AS 'percent_rank'
FROM result;
Subje Nam Ma ran total_ro percent_
cts e rk k-1 ws-1 rank

Englis Prati
h bha 70 0 2 0

Englis Ankit
h a 90 1 2 0.5

Englis Swar 10
h na 0 2 2 1

Math Ankit
s a 65 0 2 0

Math Swar
s na 85 1 2 0.5

Math Prati 10
s bha 0 2 2 1

Scien Swar
ce na 50 0 2 0

Scien Ankit
ce a 80 1 2 0.5

Scien Prati
ce bha 80 1 2 0.5
RDBMS:

RDBMS stands for Relational DataBase Management Systems. It is basically a program that allows


us to create, delete, and update a relational database. It is a database system that stores and
retrieves data in a tabular format organized in the form of rows and columns. It is a smaller subset
of DBMS which was designed by E.F Codd in the 1970s. The major DBMS like SQL Server, My-SQL,
ORACLE is based on the principles of relational DBMS.

A relational database refers to a database that stores data in a structured format,


using rows and columns. This makes it easy to locate and access specific values within the database.
It is "relational" because the values within each table are related to each other. Tables may also be
related to other tables. The relational structure makes it possible to run queries across multiple
tables at once.

Relational DBMS owes its foundation to the fact that the values of each table are related to others. It
has the capability to handle larger magnitudes of data and simulate queries easily.
Relational Database Management Systems maintains data integrity by simulating the following features:
 Entity Integrity: No two records of the database table can be completely duplicate.
 Referential Integrity: Only the rows of those tables can be deleted which are not used by other
tables. Otherwise, it may lead to data inconsistency.
 User-defined Integrity: Rules defined by the users based on confidentiality and access.
 Domain integrity: The columns of the database tables are enclosed within some structured
limits, based on default values, type of data or ranges.
Characteristics:
 Data must be stored in tabular form in DB file, that is it should be organized in the form of rows
and columns.
 Each row of table is called record/tuple . Number of such records is known as the cardinality of
the table
 Each column of the table is called an attribute/field. Number of such columns is called the arity
of the table.
 Number two records of the DB table can be same. Data duplicity is therefore avoided by using a
candidate key. Candidate Key is a minimum set of attributes required to identify the set of records
uniquely.
 Tables are related to each other with the help of foreign keys.
 Database tables also allow NULL values, that is if the values of any of the element of the table
are not filled or are missing, it becomes a NULL value, which is not equivalent to zero.

Example:
The following table STUDENT consists of three columns Roll Number, Name, Section and four
records of students 1, 2, 3 and 4 respectively. The records can’t be completely same, the Roll
Number acts as a candidate key which separates records.

Roll number Name Section

1 Anita A

2 Mark B

3 Ish A

4 Toby C
Advantages:

 Easy to manage: Each table can be independently manipulated without affecting others.
 Security: It is more secure consisting of multiple levels of security. Access of data shared can be
limited.
 Flexible: Updation of data can be done at a single point without making amendments in multiple
files. Databases can easily be extended to incorporate more records, thus providing greater scalability.
Also, facilitates easy application of SQL queries.
 Users: RDBMS supports client-side architecture storing multiple users together.
 Facilitates storage and retrieval of large amount of data.
 Easy Data Handling:
 Data fetching is faster because of relational architecture.
 Data redundancy or duplicity is avoided due to keys, indexes, and normalization
principles.
 Data consistency is ensured because RDBMS is based on ACID properties for data
transactions (Atomicity Consistency Isolation Durability).
 Fault Tolerance: Replication of databases provides simultaneous access and helps the system
recover in case of disasters, such as power failures or sudden shutdowns
Disadvantages
 High Cost and Extensive Hardware and Software Support: Huge costs and setups are required to
make these systems functional.
 Scalability: In case of addition of more data, servers along with additional power and memory
are required.
 Complexity: Voluminous data creates complexity in understanding of relations and may lower
down the performance.
 Structured Limits: The fields or columns of a relational database system is enclosed within
various limits, which may lead to loss of data

Introduction to NoSQL:

NoSQL databases (aka "not only SQL") are non tabular, and store data differently than relational tables.
NoSQL databases come in a variety of types based on their data model. The main types are document,
key-value, wide-column, and graph. They provide flexible schemas and scale easily with large amounts
of data and high user loads.
NoSQL databases can store relationship data—they just store it differently than relational databases do.
In fact, when compared with SQL databases, many find modeling relationship data in NoSQL databases
to be easier than in SQL databases, because related data doesn’t have to be split between tables.
NoSQL data models allow related data to be nested within a single data structure.
As storage costs rapidly decreased, the amount of data applications needed to store and query
increased. This data came in all shapes and sizes—structured, semistructured, and polymorphic—and
defining the schema in advance became nearly impossible. NoSQL databases allow developers to store
huge amounts of unstructured data, giving them a lot of flexibility.

Types of NoSQL database:

Over time, four major types of NoSQL databases emerged: document databases, key-value databases,
wide-column stores, and graph databases. 
 Document databases store data in documents similar to JSON (JavaScript Object Notation)
objects. Each document contains pairs of fields and values. The values can typically be a variety
of types including things like strings, numbers, booleans, arrays, or objects, and their structures
typically align with objects developers are working with in code. Because of their variety of field
value types and powerful query languages, document databases are great for a wide variety of
use cases and can be used as a general purpose database. They can horizontally scale-out to
accomodate large data volumes. MongoDB is consistently ranked as the world’s most popular
NoSQL database according to DB-engines and is an example of a document database. For more
on document databases, visit What is a Document Database?
 Key-value databases are a simpler type of database where each item contains keys and values. A
value can typically only be retrieved by referencing its key, so learning how to query for a
specific key-value pair is typically simple. Key-value databases are great for use cases where you
need to store large amounts of data but you don’t need to perform complex queries to retrieve
it. Common use cases include storing user preferences or caching. Redis and DynamoDB are
popular key-value databases.
 Wide-column stores store data in tables, rows, and dynamic columns. Wide-column stores
provide a lot of flexibility over relational databases because each row is not required to have the
same columns. Many consider wide-column stores to be two-dimensional key-value databases.
Wide-column stores are great for when you need to store large amounts of data and you can
predict what your query patterns will be. Wide-column stores are commonly used for storing
Internet of Things data and user profile data. Cassandra and HBase are two of the most popular
wide-column stores.
 Graph databases store data in nodes and edges. Nodes typically store information about people,
places, and things while edges store information about the relationships between the nodes.
Graph databases excel in use cases where you need to traverse relationships to look for patterns
such as social networks, fraud detection, and recommendation engines. Neo4j and JanusGraph
are examples of graph databases.

How NoSQL Databases Work


One way of understanding the appeal of NoSQL databases from a design perspective is to look at how
the data models of a SQL and a NoSQL database might look in an oversimplified example using address
data.
The SQL Case. For an SQL database, setting up a database for addresses begins with the logical
construction of the format and the expectation that the records to be stored are going to remain
relatively unchanged. After analyzing the expected query patterns, an SQL database might optimize
storage in two tables, one for basic information and one pertaining to being a customer, with last name
being the key to both tables. Each row in each table is a single customer, and each column has the
following fixed attributes:

 Last name :: first name :: middle initial :: address fields :: email address :: phone number
 Last name :: date of birth :: account number :: customer years :: communication preferences

The NoSQL Case. In the section Types of NoSQL Databases above, there were four types described, and
each has its own data model.
Each type of NoSQL database would be designed with a specific customer situation in mind, and there
would be technical reasons for how each kind of database would be organized. The simplest type to
describe is the document database, in which it would be natural to combine both the basic information
and the customer information in one JSON document. In this case, each of the SQL column attributes
would be fields and the details of a customer’s record would be the data values associated with each
field.
For example: Last_name: "Jones", First_name: "Mary", Middle_initial: "S", etc

Advantages:

There are many advantages of working with NoSQL databases such as MongoDB and Cassandra. The
main advantages are high scalability and high availability.

 Flexible data models


NoSQL databases typically have very flexible schemas (A schema is a collection of database
objects like tables, triggers, stored procedures, etc. A schema is connected with a user which is
known as the schema owner. Database may have one or more schema.). A flexible schema
allows you to easily make changes to your database as requirements change. You can iterate
quickly and continuously integrate new application features to provide value to your users
faster.
 Horizontal scaling
Most SQL databases require you to scale-up vertically (migrate to a larger, more expensive
server) when you exceed the capacity requirements of your current server. Conversely, most
NoSQL databases allow you to scale-out horizontally, meaning you can add cheaper, commodity
servers whenever you need to.
 Fast queries
Queries in NoSQL databases can be faster than SQL databases. Why? Data in SQL databases is
typically normalized, so queries for a single object or entity require you to join data from
multiple tables. As your tables grow in size, the joins can become expensive. However, data in
NoSQL databases is typically stored in a way that is optimized for queries. The rule of thumb
when you use MongoDB is Data that is accessed together should be stored together. Queries
typically do not require joins, so the queries are very fast.
 Easy for developers
Some NoSQL databases like MongoDB map their data structures to those of popular
programming languages. This mapping allows developers to store their data in the same way
that they use it in their application code. While it may seem like a trivial advantage, this
mapping can allow developers to write less code, leading to faster development time and fewer
bugs.

Disadvantages of NoSQL:

1. Narrow focus –
NoSQL databases have very narrow focus as it is mainly designed for storage but it provides very little
functionality. Relational databases are a better choice in the field of Transaction Management than
NoSQL.
2. Open-source –
NoSQL is open-source database. There is no reliable standard for NoSQL yet. In other words two
database systems are likely to be unequal.
3. Management challenge –
The purpose of big data tools is to make management of a large amount of data as simple as possible.
But it is not so easy. Data management in NoSQL is much more complex than a relational database.
NoSQL, in particular, has a reputation for being challenging to install and even more hectic to manage
on a daily basis.
4. GUI is not available –
GUI mode tools to access the database is not flexibly available in the market.
5. Backup –
Backup is a great weak point for some NoSQL databases like MongoDB. MongoDB has no approach for
the backup of data in a consistent manner.
6. Large document size –
Some database systems like MongoDB and CouchDB store data in JSON format. Which means that
documents are quite large (BigData, network bandwidth, speed), and having descriptive key names
actually hurts, since they increase the document size.

When should NoSQL be used:


1. When huge amount of data need to be stored and retrieved.
2. The relationship between the data you store is not that important
3. The data is changing over time and is not structured.
4. Support of Constraints and Joins is not required at database level
5. The data is growing continuously and you need to scale the database regular to handle the data.

You might also like