You are on page 1of 62

Prepared By- Mangesh Sir

VCTC,Katraj,Pune

Backend coverage testing / Database testing –

- Validating all front end operations are stored in the database or not
- Front end operation / data entered in the front end is stored in the data base or not
- We check data can be fetched or not
- For data fetch purpose there to be use SQL language
- There are different commands of SQL & by using that we can fetch data

Ex.

Project – Kite

Register page

FN/LN/Mob.No/DoB/Email Id/ Pan No/ Aadhar No

After filling form we click on submit button

Submit – message – front end – register successfully

Database – storage location – no of tables – no of rows & columns

Table name – Register

FN LN Mob.No DoB Email Id Pan No Addhar No

ABC XYZ 123 123 A@gmail.com 123 456

Language – SQL – Structured Query Language

SQL there are different commands – to fetch data

Syntax

SELECT * FROM table name;

Example

SELECT * FROM Register;

Output

FN LN Mob.No DoB Email Id Pan No AddharNo

ABC XYZ 123 123 A@gmail.com 123 456

DATA:-
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Data is a row fact which describe the attribute of an entity/object.

Piece of information

Roll No

Student FN Data

LN

Employee ID

Mangesh Department Data

Project

Object/Entity

Information:-

Processed data is called Information

Eg. VCTC- 7th Jan 2023 Morning Batch Katraj- Processed Data

Data is classified in to 2 types

1. Structured data
2. Un-structured data

Structure data:-

Data which is arranged in a structured manner or structured way it is nothing but structured
data

Ex. Data stored in a table

Column / Field / Attributes


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Rows/Records/Tuples

Examples of structured data include names, dates, addresses, credit card numbers, stock
information, and more. Structured data is highly organized and easily understood by machine
language.

Un-structured data:-

Data which is not arranged in specific structure

Examples of unstructured data has many faces like text files, PDF documents, social media
posts, comments, images, audio/video files, and emails, to name a few.
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

What is a Database?

1. A database is an organized collection of data that is structured into tables, rows,


columns, and indexes.

2. It helps the user to find the relevant information frequently.

3. It is an electronic system that makes data access, data manipulation, data retrieval,
data storing, and data management very easy.

4. Almost every organization uses the database for storing the data due to its easily
accessible and high operational case.

5. The database provides perfect access to data and lets us perform required tasks.

The following are the common features of a database:

o Manages large amounts of data


o Accurate
o Easy to update
o Security
o Data integrity
o Easy to research data

What is meant by DBMS?

1. DBMS stands for Database Management System.


2. It is a software program that primarily functions as an interface between the database
and the end-user.
3. It provides us the power such as managing the data, the database engine, and the
database schema to facilitate the organization and manipulation of data using a simple
query in almost no time.
4. It is like a File Manager that manages data in a database rather than saving it in file
systems. Without the database management system, it would be far more difficult for
the user to access the database's data.

The following are the components of a DBMS:

o Software
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

o Data
o Procedures
o Database Languages
o Query Processor
o Database Manager
o Database Engine
o Reporting

What are the different types of database management systems?

The database management systems can be categorized into several types. Some of the
important lists are given below:

o Hierarchical databases (HDBMS)


o Network databases (NDMS)
o Relational databases (RDBMS)
o Object-oriented databases
o Document databases (Document DB)
o Graph databases
o ER model databases
o NoSQL databases

What is RDBMS?

1. RDBMS stands for Relational Database Management System.

2. It is a database management system based on a relational model. It facilitates you to


manipulate the data stored in the tables by using relational operators.

3. RDBMS stores the data into the collection of tables and links those tables using the
relational operators easily whenever required.

4. Examples of relational database management systems are Microsoft Access, MySQL,


SQL Server, Oracle database, etc.

DBMS there are basic 4 types

1. Flat File database


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Here data will be stored in the form of files and folders


Advantage- Simple to store data
Disadvantages- Duplication of data

2. Hierarchical database
Here data will be stored in the form of parent – child relation or in a tree structure
Advantage- Data duplication not allowed
Disadvantages- retrieval time consuming
An example of a hierarchical database is given below –

3. Network database

Here data will be stored in a network structure. All the nodes (data) are interlinked with
another data

Advantage- retrieval data easy


Disadvantages- complex
Example
Teachers can teach in multiple departments. This is shown below −
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

4. Relational databases (RDBMS)

Here data will be stored in the form of rows & columns i.e. in a table structure
A relational database management system (RDBMS) is a system where data is
organized in two-dimensional tables using rows and columns.
This is one of the most popular data models which is used in industries. It is based on
SQL.

Every table in a database has a key field which uniquely identifies each record.
This type of system is the most widely used DBMS.

Data Integrity

It is used to get accuracy, correctness & consistency of the column of the table & it can be
achieved by data types & constraints (rules & regulation- apply on column)

Constraints-

Data Types – Refer Data Type Pdf – cover later


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

-------------------------------------------------------------------------------------------------------------------

SQL

1. 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.

2. 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?

1. SQL stands for Structured Query Language.

2. It is designed for managing data in a relational database management system


(RDBMS).

3. It is pronounced as S-Q-L or sometime See-Qwell.


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

4. SQL is a database language, it is used for database creation, deletion, fetching rows,
and modifying rows, etc.

 All DBMS like MySQL, Oracle, MS Access, Sybase, Informix, PostgreSQL, and SQL
Server use SQL as standard database language.

Why SQL is required?

SQL is required:

1. To create new databases, tables and views

2. To insert records in a database

3. To update records in a database

4. To delete records from a database

5. 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.

SQL Syntax
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

SQL follows some unique set of rules and guidelines called syntax. Here, we are providing all
the basic SQL syntax.

o SQL is not case sensitive. Generally SQL keywords are written in uppercase.

o SQL statements are dependent on text lines. We can place a single SQL statement on
one or multiple text lines.

o You can perform most of the action in a database with SQL statements.

SQL statement

SQL statements are started with any of the SQL commands/keywords like SELECT, INSERT,
UPDATE, DELETE, ALTER, DROP etc. and the statement ends with a semicolon (;).

Example of SQL statement:

1. SELECT "column_name" FROM "table_name";

Why semicolon is used after SQL statements:

Semicolon is used to separate SQL statements. It is a standard way to separate SQL statements
in a database system in which more than one SQL statements are used in the same call.

SQL Commands

These are the some important SQL command:

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.


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

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.

SQL Commands

o SQL commands are instructions. It is used to communicate with the database. It is also
used to perform specific tasks, functions, and queries of data.

o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, and set permission for users.

Types of SQL Commands / Types of SQL / Subtypes of SQL/ Subset of SQL commands

There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

1. Data Definition Language (DDL)

o DDL changes the structure of the table like creating a table, deleting a table, altering a
table, etc.

o All the command of DDL are auto-committed that means it permanently save all the
changes in the database.

Here are some commands that come under DDL:

o CREATE TABLE

o ALTER TABLE

o DROP TABLE

o TRUNCATE TABLE

2. Data Manipulation Language (DML):

o DML commands are used to modify the database. It is responsible for all form of
changes in the database.

o The command of DML is not auto-committed that means it can't permanently save all
the changes in the database. They can be rollback.

Here are some commands that come under DML:

o INSERT

o UPDATE

o DELETE

3. Data Query Language (DQL):

DQL is used to fetch the data from the database.

It uses only one command:


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

o SELECT

4. Data control language (DCL):

It controls access to the data stored in the database.

The commands in this category include

o GRANT
o REVOKE

5. Transaction Control Language (TCL):

It is used to deal with the transaction operations in the database.

The commands in this category are

o COMMIT
o ROLLBACK
o SET TRANSACTION
o SAVEPOINT

Database – In organization – Allocated on one project – Flipkart- Test lead / SM – WE have to


perform database testing – Access – TL / Teammates /SM- Raise 1 form – Project Name /
Required / PM – which tool required – Oracle – Submit – PM – approved – IT support team –
contact you – They install – Oracle

Database commands – provided by developer

Database -

A database contains one or more tables & each table identified by a name (eg. Signup, Register,
Login credentials, Employee info, Students info……etc.)

Each table contains multiple records (rows) with data

Example:-

Front end – Sign Up

ID-
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

FN-

LN-

MobNo-

EmailId-

City-

Submit Button – Successfully SignUp

Backend - Database- Table Name- SignUp

ID FN LN MobNo EmailId City

1 Rahul Gandhi 1111 1@gmail.co Pune


m

2 Arvind Kejriwal 2222 2@gmail.co Mumbai


m

3 Anna Hajare 3333 3@gmail.co Pune


m

4 Sharad Pawar 4444 4@gmail.co Baramati


m

5 Soniya Gandhi 5555 5@gmail.co Pune


m

Above table contains 5 records (rows) & 6 columns

Oracle Installation – Done – User Credentials – UN & PW

1. SELECT

- The SQL SELECT Statement is used to select data from a table / database
- Ex. Select all the records in the Sign Up table
A. Select all data from the table / Select all table information / fetch all table information /
retrieve all table information

Syntax
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

SELECT * FROM table_name;

Example

SELECT * FROM SignUp;

Output

ID FN LN MobNo EmailId City

1 Rahul Gandhi 1111 1@gmail.co Pune


m

2 Arvind Kejriwal 2222 2@gmail.co Mumbai


m

3 Anna Hajare 3333 3@gmail.co Pune


m

4 Sharad Pawar 4444 4@gmail.co Baramati


m

5 Soniya Gandhi 5555 5@gmail.co Pune


m

Note – Meaning of “*” – Defines all fields are present

B. SELECT particular column

Syntax

SELECT Column name 1, column name 2……column name N

FROM table_name;

Example

SELECT FN, LN

FROM SignUp;

Output -

FN LN
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

1 Rahul Gandhi

2 Arvind Kejriwal

3 Anna Hajare

4 Sharad Pawar

5 Soniya Gandhi
System generated column – Oracle

2. DISTINCT
- The SQL DISTINCT statements used to return / show only distinct value (Unique value /
different value (Duplicate value are not present)) in the particular column
- Inside a table, a column contains many duplicate values, & sometimes we wants to list
of the only unique value
- Ex. Select only the distinct value from the LN column in the SignUp table

Syntax

SELECT DISTINCT column name

FROM table_name;

Example

SELECT DISTINCT LN

FROM SignUp;

Output

LN

1 Gandhi

2 Kejriwal

3 Hajare

4 Pawar

Without Distinct
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Syntax

SELECT column name

FROM table_name;

Example

SELECT LN

FROM SignUp;

Output

LN

1 Gandhi

2 Kejriwal

3 Hajare

4 Pawar

5 Gandhi

Find the number of unique records

Syntax

SELECT COUNT (DISTINCT column name)

FROM table name;

Example

SELECT COUNT (DISTINCT LN)

FROM SignUp;

Output

COUNT(DISTINC
T LN)

1 4
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

3. TOP/ ROWNUM/LIMIT
- It is SQL statement used to specify the number of record to return
- It is a SQL statements used to show/display/return the top records in a particular table
- It is SQL statements use to select top values/records in particular table

TOP – SQL Server

Syntax

SELECT TOP value * FROM table name;

Example

SELECT TOP 3 * FROM SignUp;

Output

ID FN LN MobNo EmailId City

1 1 Rahul Gandhi 1111 1@gmail.com Pune

2 2 Arvind Kejriwal 2222 2@gmail.com Mumbai

3 3 Anna Hajare 3333 3@gmail.com Pune

Syntax

SELECT TOP value PERCENT * FROM table name;

Example

SELECT TOP 50 Percent * FROM SignUp;

Output

ID FN LN MobNo EmailId City

1 1 Rahul Gandhi 1111 1@gmail.com Pune

2 2 Arvind Kejriwal 2222 2@gmail.com Mumbai

3 3 Anna Hajare 3333 3@gmail.com Pune


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

ROWNUM – Oracle

Syntax

SELECT * FROM table name

WHERE ROWNUM<=value;

Example

SELECT * FROM SignUp

WHERE ROWNUM<=3;

Output

ID FN LN MobNo EmailId City

1 1 Rahul Gandhi 1111 1@gmail.com Pune

2 2 Arvind Kejriwal 2222 2@gmail.com Mumbai

3 3 Anna Hajare 3333 3@gmail.com Pune

LIMIT- MySQL

Syntax

SELECT * FROM table name

LIMIT value;

Example

SELECT * FROM SIgnUp

LIMIT 3;

Output

ID FN LN MobNo EmailId City

1 1 Rahul Gandhi 1111 1@gmail.com Pune


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

2 2 Arvind Kejriwal 2222 2@gmail.com Mumbai

3 3 Anna Hajare 3333 3@gmail.com Pune

4. ORDER BY
- It is a SQL statements used to sort the result set in ascending order and descending order
- The ORDER BY keywords sorts the records in ascending order by default or use the ASC
keywords
- To sorts the records in descending order to use the DESC keywords
- It is used to display records by ascending order and descending order from selected
column

Ascending Descending

1 2 3 4 5 5 4 3 2 1

A B C D E E D C B A

1. Ascending order

Syntax

SELECT * FROM table name or SELECT * FROM table name

ORDER BY column name; ORDER BY column name ASC;

Example

SELECT * FROM SignUp or SELECT * FROM SignUp

ORDER BY FN; ORDER BY FN ASC;

Output

ID FN LN MobNo EmailId City

1 3 Anna Hajare 3333 3@gmail.com Pune


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

2 2 Arvind Kejriwal 2222 2@gmail.com Mumbai

3 1 Rahul Gandhi 1111 1@gmail.com Pune

4 4 Sharad Pawar 4444 4@gmail.com Baramati

5 5 Soniya Gandhi 5555 5@gmail.com Pune

2. Descending order
Syntax
SELECT * FROM table name
ORDER BY Column name DESC;

Example
SELECT * FROM SignUp
ORDER BY FN DESC;

Output

ID FN LN MobNo EmailId City

2 5 Soniya Gandhi 5555 5@gmail.com Pune

1 4 Sharad Pawar 4444 4@gmail.com Baramati

3 1 Rahul Gandhi 1111 1@gmail.com Pune

4 2 Arvind Kejriwal 2222 2@gmail.com Mumbai

5 3 Anna Hajare 3333 3@gmail.com Pune

5. WHERE clause
- The WHERE clause used to filter out the records
- It is used to extract only those records that full filled a specified condition

Ex. SignUp- City = ‘Pune’

Syntax
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

SELECT * FROM table name

WHERE Condition;

Condition – Manes Column name = value

Example

SELECT * FROM SignUp

WHERE City = ‘Pune’;

Output

ID FN LN MobNo EmailId City

1 1 Rahul Gandhi 1111 1@gmail.com Pune

2 3 Anna Hajare 3333 3@gmail.com Pune

3 5 Soniya Gandhi 5555 5@gmail.com Pune

Example

SELECT * FROM SignUp

WHERE ID = 1;

Output

ID FN LN MobNo EmailId City

1 1 Rahul Gandhi 1111 1@gmail.com Pune

Syntax

SELECT Column name 1, column name 2…..

FROM table name

WHERE condition;

Example
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

SELECT Email Id

FROM SignUp

WHERE City=’Pune’;

Output

Email Id
1 1@gmail.com
2 3@gmail.com
3 5@gmail.com

Under the WHERE clause there are different operators

Different Operators

1. OR
2. AND
3. LIKE
4. WILDCARD
5. NOT
6. IN
7. BETWEEN

OR

- it is a SQL statements use to select records when both condition is true or one condition
is true

- it is a SQL statements use to select records either one condition must be true then output
is true

1 1 = 1

1 0 = 1

0 1 = 1
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

0 0 = 0

Syntax

SELECT * FROM table name

WHERE condition 1 OR condition 2;

Condition means- Column name = value;

Example

SELECT * FROM SignUp

WHERE FN=’Rahul’ OR LN=’Gandhi’;

Output

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 1@gmail.com Pune


2 5 Soniya Gandhi 5555 5@gmail.com Pune

Syntax

SELECT column name 1, column name 2…….

FROM table name

WHERE condition 1 OR condition 2;

Example

SELECT Email Id

FROM SignUp

WHERE FN=’Rahul’ OR LN=’Gandhi’;

Output

Email Id
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

1 1@gmail.com
2 5@gmail.com

AND

- it is a SQL statements in that select records when both the conditions must be true then
output will be true

1 1 = 1

0 1 = 0

1 0 = 0

0 0 = 0

Syntax

SELECT * FROM table name

WHERE condition 1 AND condition 2;

Example

SELECT * FROM SignUp

WHERE FN = ‘Rahul’ AND LN = ‘Gandhi’;

Output

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 1@gmail.com Pune

Syntax
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

SELECT column name 1…………….

FROM table name

WHERE condition 1 AND condition 2;

Example

SELECT Mob.No

FROM SignUp

WHERE FN = ‘Rahul’ AND LN = ‘Gandhi’;

Output

Mob. No

1 1111

LIKE

Mangesh _ang___ =

- The like operator is used in WHERE clause to search a specified pattern in a column
- In like operator use % sign / symbol
a. For starting with specified alphabets

Syntax

SELECT * FROM table name

WHERE column name LIKE pattern; or char%;

Example

SELECT * FROM SignUp


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

WHERE FN LIKE ‘A%’;

Output

ID FN LN Mob. No Email Id City

1 2 Arvind Kejriwal 2222 2@gmail.com Mumbai


2 3 Anna Hajare 3333 3@gmail.com Pune

Syntax

SELECT column name

FROM table name

WHERE column name LIKE pattern; or char%;

Example

SELECT LN

FROM SignUp

WHERE FN LIKE ‘A%’;

Output

LN

1 Kejriwal
2 Hajare

b. For ending with specified alphabets

Mangesh -----------------------------h

Syntax

SELECT * FROM table name

WHERE column name LIKE pattern; or %char;


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Example

SELECT * FROM SignUp

WHERE FN LIKE ‘%d’;

Output

ID FN LN Mob. No Email Id City

1 2 Arvind Kejriwal 2222 2@gmail.com Mumbai


2 4 Sharad Pawar 4444 4@gmail.com Baramati

Syntax

SELECT column name

FROM table name

WHERE column name LIKE pattern; or %char;

Example

SELECT LN

FROM SignUp

WHERE FN LIKE ‘%d’;

Output

LN

1 Kejriwal
2 Pawar

NOT

Syntax

SELECT * FROM table name

WHERE column name NOT LIKE pattern;


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Example

SELECT * FROM SignUp

WHERE FN NOT LIKE ‘A%’;

Output

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 1@gmail.com Pune


2 4 Sharad Pawar 4444 4@gmail.com Baramati
3 5 Soniya Gandhi 5555 5@gmail.com Pune

Ex.

Arvind = A%d

Ex.

Arvind = %vin%

Select all records where the value of the column starts with the letter "A".

Syntax

SELECT * FROM table_name


WHERE column LIKE ‘A%’;

Select all records where the value of the column ends with the letter "a".

Syntax
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

SELECT * FROM table_name


WHERE column LIKE ‘%a’;

Select all records where the value of the column starts with letter "A" and ends with the letter
"b".

Syntax

SELECT * FROM table_name


WHERE column LIKE ‘A%b’;

Select all records where the value of the column contains the letter "a".

Syntax

SELECT * FROM table_name


WHERE column LIKE ‘%a%’;

Select all records where the value of the column does NOT start with the letter "A".

Syntax

SELECT * FROM table_name


WHERE column NOT LIKE ‘A%’;

For Starting with alphabets ABC

The following SQL statement selects all Sign Up with a FN starting with "A", "B", or "C":

Syntax

SELECT * FROM table_name


WHERE column LIKE ‘[ABC]%’;

For Ending with alphabets ABC


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

The following SQL statement selects all Sign Up with a FN ending with "A", "B", or "C":

Syntax

SELECT * FROM table_name


WHERE column LIKE ‘%[ABC]’;

LIKE Operator Description

WHERE Column LIKE 'a%' Finds any values that start with "a"

WHERE Column LIKE '%a'


Finds any values that end with "a"

WHERE Column LIKE '%or%' Finds any values that have "or" in any
position

WHERE Column LIKE '_r%' Finds any values that have "r" in the second
position

WHERE Column LIKE 'a_%' Finds any values that start with "a" and are
at least 2 characters in length

WHERE Column LIKE 'a__%' Finds any values that start with "a" and are
at least 3 characters in length

WHERE Column LIKE 'a%o' Finds any values that start with "a" and ends
with "o"

WILDCARD

Ex.

Mangesh
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Man____ Man__sh ___gesh __nge__

- A wildcard is used to substitute one or more char in a string


- Wildcard are used with like operator
- The like operator is used in a WHERE clause to search a specified pattern in a column
- In wildcard use ‘_’ sign / symbol

Syntax

SELECT * FROM table name

WHERE column name LIKE pattern;

Example

SELECT * FROM table name

WHERE FN LIKE ‘_ahul’;

Output

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 1@gmail.com Pune

Rahul

Aahul

IN

- IN operator allows you to specify multiple value in a WHERE clause


- It is used to fetch one or more data from the table
- It is use to select those records which specify in query

Syntax

SELECT * FROM table name

WHERE column name IN (Value 1, Value 2, Value 3……N);


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Example

SELECT * FROM SignUp

WHERE FN IN (‘Rahul’,’Soniya’);

Output

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 1@gmail.com Pune

2 5 Soniya Gandhi 5555 5@gmail.com Pune

NOT

Syntax

SELECT * FROM table name

WHERE column name NOT IN (Value 1, Value 2, Value 3……N);

Example

SELECT * FROM SignUp

WHERE FN NOT IN (‘Rahul’,’Soniya’);

Output

ID FN LN Mob. No Email Id City

1 2 Arvind Kejriwal 2222 2@gmail.com Mumbai

2 3 Anna Hajare 3333 3@gmail.com Pune

3 4 Sharad Pawar 4444 4@gmail.com Baramati

Examples

SELECT * FROM SignUp

WHERE ID IN (1,4,5);

Output
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Examples

SELECT * FROM SignUp

WHERE City IN (‘Pune’,’Baramati’);

Output

BETWEEN

- The BETWEEN Operator selects value within a given range


- The value can be number, text, & date
- The between operator defines- begin / start value and end value are included
- It is SQL statements use to select value between range which specified

Syntax

SELECT * FROM table name

WHERE column name BETWEEN value 1 AND value 2;

Example

SELECT * FROM SignUp

WHERE ID BETWEEN 2 AND 4;

Output

ID FN LN Mob. No Email Id City

1 2 Arvind Kejriwal 2222 2@gmail.com Mumbai

2 3 Anna Hajare 3333 3@gmail.com Pune

3 4 Sharad Pawar 4444 4@gmail.com Baramati

Example

SELECT * FROM SignUp

WHERE Email ID BETWEEN ‘1@gmail.com’ AND ‘3@gmail.com’;

Output
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

ID FN LN Mob. No Email Id City


1 Rahul Gandhi 1111 1@gmail.com Pune

2 Arvind Kejriwal 2222 2@gmail.com Mumbai

3 Anna Hajare 3333 3@gmail.com Pune

Example

SELECT * FROM SignUp

WHERE FN BETWEEN ‘Arvind’ AND ‘Soniya’;

ID FN LN Mob. No Email Id City

1 2 Arvind Kejriwal 2222 2@gmail.com Mumbai

2 3 Anna Hajare 3333 3@gmail.com Pune

3 4 Sharad Pawar 4444 4@gmail.com Baramati

4 5 Soniya Gandhi 5555 5@gmail.com Pune

NOT BETWEEN Example

To display the products outside the range of the previous example, use NOT BETWEEN:

It is a SQL statement use to select value between ranges which specified.

Syntax

SELECT * FROM table_name


WHERE Column_name NOT BETWEEN Value 1 AND Value 2;

Example

SELECT * FROM SignUp


WHERE ID NOT BETWEEN 2 AND 4;
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Output

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 1@gmail.com Pune

2 5 Soniya Gandhi 5555 5@gmail.com Pune


SELECT

DISTINCT

TOP

ORDER BY

WHERE CLAUSE

Operators

1. OR
2. AND
3. LIKE
4. WILDCARD
5. NOT
6. IT
7. BETWEEN

INSERT INTO

UPDATE

DELETE

ALIAS

SELECT INTO
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

UNION & UNION ALL

CREATE TABLE

DROP TABLE

TRUNCATE TABLE

ALTER TABLE

CONSTRAINTS

JOIN

AGGREGATE FUNCTION

GROUP BY

HAVING

FIND NTH SALARY

INTERNVIEW QUESTIONS
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

INSERT INTO

- The insert into keywords is used to insert new records in a table


a. Insert the value in to all column – insert new records

Syntax

INSERT INTO table name

VALUES (value 1, value 2, value 3……..);

Example

INSERT INTO SignUp

VALUES (6, ‘Jayant’, ‘Patil’, 6666, ‘6@gmail.com’, ‘Sangli’);

Output

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 1@gmail.com Pune


2 2 Arvind Kejriwal 2222 2@gmail.com Mumbai
3 3 Anna Hajare 3333 3@gmail.com Pune
4 4 Sharad Pawar 4444 4@gmail.com Baramati
5 5 Soniya Gandhi 5555 5@gmail.com Pune
6 6 Jayant Patil 6666 6@gmail.com Sangli

b. Insert values in to the particular column – insert records in particular column

Syntax

INSERT INTO table name column name 1, column name 2, column 3…..

VALUES (value 1, value 2, value 3……..);


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Example

INSERT INTO SIgnUp FN, LN, Mob.No

VALUES (‘Amit’, ‘Deshmukh’, 7777);

Output

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 1@gmail.com Pune

2 2 Arvind Kejriwal 2222 2@gmail.com Mumbai


3 3 Anna Hajare 3333 3@gmail.com Pune
4 4 Sharad Pawar 4444 4@gmail.com Baramati
5 5 Soniya Gandhi 5555 5@gmail.com Pune
6 6 Jayant Patil 6666 6@gmail.com Sangli
7 NULL Amit Deshmukh 7777 NULL NULL

UPDATE

- The update keywords is used to modify the existing records in a table

Syntax

UPDATE table name

SET column1 = value, column name 2 = value 2……..

WHERE condition;

Condition – means column name = value

Example

UPDATE SignUp

SET Mob.No=0000, Email=’0@gmail.com’, City=’Delhi’

WHERE FN=’Rahul’;
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Output

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 0000 0@gmail.com Delhi


2 2 Arvind Kejriwal 2222 2@gmail.com Mumbai
3 3 Anna Hajare 3333 3@gmail.com Pune
4 4 Sharad Pawar 4444 4@gmail.com Baramati
5 5 Soniya Gandhi 5555 5@gmail.com Pune

Note- Missing WHERE clause = all records in the table will be updated by given set value

Syntax

UPDATE table name

SET column1 = value, column name 2 = value 2……..;

Example

UPDATE SignUp

SET Mob.No=0000, Email=’0@gmail.com’, City=’Delhi’;

Output

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 0000 0@gmail.com Delhi


2 2 Arvind Kejriwal 0000 0@gmail.com Delhi
3 3 Anna Hajare 0000 0@gmail.com Delhi
4 4 Sharad Pawar 0000 0@gmail.com Delhi
5 5 Soniya Gandhi 0000 0@gmail.com Delhi
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

DELETE

- The delete keyword is used to delete existing records in a table


- It is a SQL statements used to delete the particular row / record in to the table

Syntax

DELETE FROM table name

WHERE condition;

Example

DELETE FROM SIgnUp

WHERE Mob.No=2222;

Output

ID FN LN Mob. No Email Id City


1 Rahul Gandhi 1111 1@gmail.com Delhi
3 Anna Hajare 3333 3@gmail.com Pune
4 Sharad Pawar 4444 4@gmail.com Baramati
5 Soniya Gandhi 5555 5@gmail.com Pune

Note- Missing WHERE clause = All records in the table will be deleted – delete all records

Syntax

DELETE FROM table name

Example

DELETE FROM SignUp

Output

ALL records present in the sign up table will be deleted


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Note- WHERE clause use with SELECT, UPDATE, DELETE

ALIAS

Ex.

Table – column name – gggggggggggggggggggggggggggggggggggggggggggg

- It is a SQL statements – used to change the temporary column name or table name
without changing in the database

First Name – FN

- Use AS keyword
- Adv- Column name are wide range or not readable properly

Syntax

SELECT old column name AS new column name

FROM table name;

Example

SELECT FirstName AS FN, Last Name AS LN

FROM SignUp;

Output

FN LN
Rahul Gandhi
Arvind Kejriwal
Anna Hajare
Sharad Pawar
Soniya Gandhi

Syntax

SELECT old column name AS new column name


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

FROM old table name AS new table name;

Example

SELECT FirstName AS FN, Last Name AS LN

FROM SignUp AS SU;

Output

Table Name = SU

FN LN
Rahul Gandhi
Arvind Kejriwal
Anna Hajare
Sharad Pawar
Soniya Gandhi

Note- To change column name perment – RENAME

SELECT INTO

- The SELECT into keyword is used to copies data from one table in to another table

Syntax

SELECT * INTO new table name FROM old table name;

Example

SELECT * INTO Register FROM SignUp;


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Output

Table Name – Register

ID FN LN Mob. No Email Id City


1 Rahul Gandhi 1111 1@gmail.com Pune
2 Arvind Kejriwal 2222 2@gmail.com Mumbai
3 Anna Hajare 3333 3@gmail.com Pune
4 Sharad Pawar 4444 4@gmail.com Baramati
5 Soniya Gandhi 5555 5@gmail.com Pune

SELECT * FROM Register;

ID FN LN Mob. No Email Id City


1 Rahul Gandhi 1111 1@gmail.com Pune
2 Arvind Kejriwal 2222 2@gmail.com Mumbai
3 Anna Hajare 3333 3@gmail.com Pune
4 Sharad Pawar 4444 4@gmail.com Baramati
5 Soniya Gandhi 5555 5@gmail.com Pune

UNION & UNION ALL

Table 1 – LN– 5 records

TN1/LN & TN2/LN combine-

Table 2 – LN – 5 records

Union – Select only unique value

Union All- Select all records


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

- In UNION is used to combine the result set of two or more select statements
- Condition
1. The column must have similar data type
2. Every select statement within union have the same no of column
3. The column in every select statements must have the same order

Example

Table Name- Sign up

ID FN LN Mob. No Email Id City Seats


1 Rahul Gandhi 1111 1@gmail.com Pune 45
2 Arvind Kejriwal 2222 2@gmail.com Mumbai 49
3 Anna Hajare 3333 3@gmail.com Pune 45
4 Sharad Pawar 4444 4@gmail.com Baramati 55
5 Soniya Gandhi 5555 5@gmail.com Pune 49

Table Name- Register

ID FN LN Mob. No Email Id City Seats


1 Rahul Gandhi 1111 1@gmail.com Pune 45
2 Arvind Kejriwal 2222 2@gmail.com Mumbai 49
3 Anna Hajare 3333 3@gmail.com Pune 45
4 Sharad Pawar 4444 4@gmail.com Baramati 55
5 Soniya Gandhi 5555 5@gmail.com Pune 49
6 Sanjay Raut 66666 6@gmail.com Mumbai 53
7 Uddhav Thakre 7777 7@gmail.com Mumbai 54
8 Rohit Pawar 8888 8@gmail.com Baramati 56
9 Dhiraj Deshmukh 0000 9@gmail.com Latur 57
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Syntax

SELECT column name FROM table name 1

UNION

SELECT column name FROM table name 2;

Example

SELECT LN FROM SignUp

UNION

SELECT LN FROM Register;

Output

LN

1 Deshmukh
2 Gandhi
3 Hajare
4 Kejriwal
5 Pawar
6 Raut
7 Thakre

Syntax

SELECT column name FROM table name 1

UNION ALL

SELECT column name FROM table name 2;

Example

SELECT LN FROM SignUp

UNION
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

SELECT LN FROM Register;

Output

LN

1 Deshmukh
2 Gandhi
3 Gandhi
4 Gandhi
5 Gandhi
6 Hajare
7 Hajare
8 Kejriwal
9 Kejriwal
10 Pawar
11 Pawar
12 Pawar
13 Raut
14 Thakre

==================================================================
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

CREATE TABLE

- The create table keyword is used to create a new table in a database

Syntax

CREATE TABLE table name

Column name 1 data type,

Column name 1 data type,

Column name 1 data type,

Column name 1 data type,

Nth

);

Example

CREATE TABLE SignUp

ID int,

FN varchar(255),

LN varchar(255),

Mob.No int,

Email Id varchar(255),

City varchar(255)

);
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Output

Table SignUp created

ID FN LN Mob. No Email Id City

SELECT * FROM SignUp;

DROP TABLE

- The drop table keyword is used to drop an existing table in a database


- It is used to drop/delete both structure and records present in the table

Syntax

DROP TABLE table name;

Example

DROP TABLE SignUp;

Output

SignUp table dropped

Delete both structure & records stored in the table- database

TURNCATE TABLE

- The truncate table keywords is used to delete the data inside a table but not the table
itself
- It is used to delete all the rows / records from the table
- It is used to delete all stored records in the table but the structure remains same
- Not delete the structure of the table

Syntax

TRUNCATE TABLE table name;


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Example

TRUNCATE TABLE SignUp;

Output

Table SingU Truncated

ID FN LN Mob. No Email Id City

DROP

1. Drop is a DDL command


2. We can not use where clause with drop
3. Drop statements used to drop table in the database
4. Drop faster than truncate and delete
5. It is not possible to rollback after using DROP Statements

ALTER TABLE

- The alter table is used to add column, drop column, & modify column in an existing
table
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

1. ALTER TABLE – ADD


- To add a new column in a table

Syntax

ALTER TABLE table name

ADD column name data type;

Example

ALTER TABLE SignUP

ADD Address varchar(255);

Output

Sign table altered

ID FN LN Mob. No Email Id City Address

1 1 Rahul Gandhi 1111 1@gmail.com Pune

2 2 Arvind Kejriwal 2222 2@gmail.com Mumbai

3 3 Anna Hajare 3333 3@gmail.com Pune

4 4 Sharad Pawar 4444 4@gmail.com Baramati

5 5 Soniya Gandhi 5555 5@gmail.com Pune

2. ALTER TABLE – DROP COLUMN


- To delete a column in a table

Syntax

ALTER TABLE table name

DROP COLUMN column name;

Example

ALTER TABLE SignUp

DROP COLUMN Address;


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Output

SignUp table altered

ID FN LN Mob. No Email Id City

1 1 Rahul Gandhi 1111 1@gmail.com Pune


2 2 Arvind Kejriwal 2222 2@gmail.com Mumbai
3 3 Anna Hajare 3333 3@gmail.com Pune
4 4 Sharad Pawar 4444 4@gmail.com Baramati
5 5 Soniya Gandhi 5555 5@gmail.com Pune

3. ALTER TABLE – ALTER COLUMN / MODIFY COLUMN / MODIFY


- To change the data type of column in a table

SQL Server / MS access – Database

Syntax

ALTER TABLE table name

ALTER COLUMN column name datatype;

MySQL/ Oracle (Prior version 10g)

Syntax

ALTER TABLE table name

MODIFY COLUMN column name datatype;

Oracle 11g & later

Syntax

ALTER TABLE table name

MODIFY column name datatype;


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Example

ALTER TABLE SignUp

MODIFY FN char(10);

Output

SignUp table altered

FN data type = varchar(255) change to char (10)

=======================================================================

Constraints

Rules / regulation – limitation – to column – for data entered

- Constraints are the rules enforced on data column on table


- These are used to limit the type of data that can go into a table / column
- SQL constraints are used to specify rules for the data in a table
- Constraints are used to limit the type of data that can go into a table

Syntax

CREATE TABLE table name

Column name 1 data type constraints,

Column name 1 data type constraints,

Column name 1 data type constraints,

Column name 1 data type constraints,

Nth

);
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

There are different type of constraints

- NULL
- NOT NULL
- UNIQUE KEY
- PRIMARY KEY
- FOREGIN KEY
- CHECK
- DEFAULT

Example

Example

CREATE TABLE SignUp

ID int NOT NULL, UNIQUE KEY,

FN varchar(255) NOT NULL,

LN varchar(255) NOT NULL,

Mob.No int NOT NULL, UNIQUE KEY,

Email Id varchar(255) NOT NULL, UNIQUE KEY,

City varchar(255) NOT NULL, DEFAULT=’Pune’,

Age int NOT NULL, CHECK (Age)>=22,

PRIMARY KEY (ID)

);

Constraints Types-

NULL

- Ensure that column can accept NULL value


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

NOT NULL

- Ensure that column can not accept NULL value


- NULL is not allowed for that column

CREATE TABLE SignUp

ID int NOT NULL,

FN varchar(255) NOT NULL,

LN varchar(255) NOT NULL,

Mob.No int NOT NULL,

Email Id varchar(255) NOT NULL,

City varchar(255) NOT NULL,

Age int NOT NULL,

);

UNIQUE KEY

- Ensure that All values in a column are unique / different


- Ensure that UNIQUE KEY constraints allows only one NULL value
- Each table have more than one unique key

Example

CREATE TABLE SignUp

ID int UNIQUE KEY,

FN varchar(255),

LN varchar(255),

Mob.No int UNIQUE KEY,


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Email Id varchar(255) UNIQUE KEY,

City varchar(255),

Age int,

);

Primary Key

- A PK constraints -uniquely identified each records in a table


- Primary key must contains unique values
- A primary key column can not allowed NULL value
- A table can have only one primary key

Note- When multiple filed are used a primary key, they are called a Composite key

CREATE TABLE SignUp

ID int ,

FN varchar(255) ,

LN varchar(255) ,

Mob.No int ,

Email Id varchar(255) ,

City varchar(255) ,

Age int ,

PRIMARY KEY (ID)

);

Foreign KEY

- A foreign key is a SQL constraints used to link two or more table


- Foreign key can accept multiple NULL value
- A table have more than one foreign key
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

- It is also called as referencing key

Example

CREATE TABLE UIDAI

AADHAR NO int(25),

FIRSTNAME varchar(256) NOT NULL,

LASTNAME varchar(256),

MOBILE int(20) UNIQUE,

EMAIL ID varchar(256) UNIQUE,

PAN CARD varchar(25) UNIQUE,

CITY varchar(25),

PRIMARY KEY (AADHAR NO)

CREATE TABLE ICICI

FIRSTNAME varchar(256),

LASTNAME varchar(256),

BALANCE int (20),

ACCOUNT NO int (20),

AADHAR NO int (20),

FOREIGN KEY (AADHAR NO) REFERENCE UIDAI (AADHAR NO)


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

CREATE TABLE IDBI

AADHAR NO. int (20),

BALANCE int (20),

FOREIGN KEY (AADHAR NO) REFERENCE UIDAI (AADHAR NO)

Notice that the "AADHAR NO" column in the "ICICI", "AADHAR NO" column in the "IDBI"
table points to the "AADHAR NO" column in the "UIDAI" table.

The "AADHAR NO" column in the "UIDAI" table is the PRIMARY KEY in the "UIDAI" table.

The "AADHAR NO" column in the "ICICI" table is a FOREIGN KEY in the "ICICI" table.

The "AADHAR NO" column in the "IDBI" table is a FOREIGN KEY in the "IDBI" table.

Default

- Set a default value for a column if not value is specified


- The default constraints provides a default value to a column when insert into statements
does not provide a specific value

CREATE TABLE SignUp

ID int ,

FN varchar(255) ,

LN varchar(255) ,

Mob.No int ,

Email Id varchar(255) ,
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

City varchar(255) DEFAULT ’Pune’,

Age int ,

);

Check

- Ensure that the value in a column satisfies a specific condition


- To check constraints is used to limit the value that can be entered in a column

CREATE TABLE SignUp

ID int ,

FN varchar(255) ,

LN varchar(255) ,

Mob.No int ,

Email Id varchar(255) ,

City varchar(255) ,

Age int CHECK (Age)>=22,

);

What is constraints?

What are different types of constraints?

What is difference between unique key, primary key & foreign key?

What is default constraints?

What is check constraints?


Prepared By- Mangesh Sir
VCTC,Katraj,Pune

Difference between Primary key, Foreign key & Unique Key

Primary Key Foreign Key Unique Key


The PRIMARY KEY A FOREIGN KEY is a key used to The UNIQUE
constraint uniquely link two tables together. A FOREIGN constraint ensures
identifies each record in a KEY is a field (or collection of fields) that all values in a
table. Primary keys must in one table that refers to the column are different.
contain UNIQUE values PRIMARY KEY in another table.

Primary key cannot have a Foreign key can accept multiple Unique Constraint
NULL value. NULL value. may have a NULL
value.

Each table can have only We can have more than one foreign Each table can have
one primary key. key in a table. more than one
Unique Constraint.

Difference between- NULL, Blank space, Zero?

=======================================================================

JOINS

- JOIN is used to join two or more table on primary key & foreign key
- JOIN is a SQL statements it is used to join two or more table based on the basic
relationship between primary key & foreign key
- The SQL joins is used to combine records from two or more tables in a database
- A JOIN means combining fields from two tables by using values common to each

There are different types of JOIN

1. INNER JOIN
2. LEFT JOIN
3. RIGHT JOIN
4. FULL JOIN
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

INNER JOIN

- Returns records when there is a match in both tables


- Select/ Returns records that have matching values in both tables

LEFT JOIN

- Return all records from the left table & the matching records from both table / right
table

RIGHT JOIN

- Return all records from the right table & the matching records from both table / left
table

FULL JOIN
Prepared By- Mangesh Sir
VCTC,Katraj,Pune

- Return all records from the left table & right table & the matching records from both
table

=======================================================================

Aggregate Functions

Group By

Having

Find Salary?

You might also like