@@database (SQL)
@@database (SQL)
- Behavior coverage
- Input domain coverage
- Error handling coverage
- Service level coverage
- Calculation based Coverage
- Backend Coverage(Database coverage/Database Testing)
Database Systems:
Database: Storage Location: Collection of related data.
- Structured Data- IRCTC, Facebook, Flipkart, Amazon, MSEB Board, MPSC
- Unstructured Data- Random Data—Social Media Post- Data can not be stored
in db in the form of Database.
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?
• All DBMS like MySQL, Oracle, MS Access, Sybase, Informix, PostgreSQL, and SQL
Server use SQL as standard database language.
SQL is required:
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
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 (;).
SQL Commands
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.
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
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.
o CREATE
o ALTER
o DROP
o TRUNCATE
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
o SELECT
Database Tables
A database most often contains one or more tables. Each table is identified by a name (e.g. "Sign
Up" or "Login Credentials" or “Orders” or “Employ Info”). Tables contain records (rows) with
data.
The table above contains five records (one for each user) and 6 columns (ID, FN, LN, MobNo,
EmailId, & City).
1. SQL SELECT Statement
The following SQL statement selects or fetch all the records in the "SignUp" table:
The following SQL statement selects all the columns from the "SignUp" table:
SELECT Syntax
Example
Output / Result
The following SQL statement selects or fetch the particular column data / records in the
"Sign Up" table:
The following SQL statement selects the "FN" and "LN" columns from the "SignUp" table:
Syntax
SELECT FN, LN
FROM SignUp;
Output / Result
FN LN
1 Rahul Gandhi
2 Arvind Kejriwal
3 Anna Hajare
4 Sharad Pawar
5 Soniya Gandhi
2. DISTINCT
The SELECT DISTINCT statement is used to return / show only distinct (different) values or
unique values in the particular column.
Inside a table, a column often contains many duplicate values; and sometimes you only want to
list the different (distinct) values.
The following SQL statement selects only the DISTINCT values from the "LN" column in the
"SignUp" table:
Syntax
Example
SELECT DISTINCT LN
FROM SignUp;
Output / Result
LN
1 Gandhi
2 Kejriwal
3 Hajare
4 Pawar
The following SQL statement selects all (including the duplicates) values from the "LN" column
in the "SignUp" table:
Syntax
SELECT Column1
FROM table_name;
Example
SELECT LN
FROM SignUp;
Output / Result
LN
1 Gandhi
2 Kejriwal
3 Hajare
4 Pawar
5 Gandhi
The following SQL statement lists the number of different (distinct) LN:
Syntax
Example
Output / Result
Number of Records-4
3. TOP
It is a SQL statement it used to show / display the top records in a particular column.
The following SQL statement selects the first three records from the "Signup" table (for SQL
Server/MS Access):
Syntax
Example
Output
Syntax
Example
Output
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.
It is a SQL statement it used to display the records by ascending & descending order from
selected column.
The following SQL statement selects all SignUp from the "SignUp" table, sorted by the "FN"
column:
Syntax
Example
Output
The following SQL statement selects all SignUp from the "SignUp" table, sorted DESCENDING
by the "FN" column:
Syntax
Example
Output
Note: The WHERE clause is not only used in SELECT statements, it is also used
in UPDATE, DELETE, etc.!
The following SQL statement selects all the SignUp from the City "Pune", in the "SignUp" table:
Syntax
Example
Output
Example
Output
Syntax
Example
SELECT EmailId
FROM SignUp
WHERE City=’Pune’;
Output
Email Id
1 [email protected]
2 [email protected]
3 [email protected]
Types of Where Clause
1. OR
2. AND
The AND and OR operators are used to filter records based on more than one condition:
• The AND operator displays a record if all the conditions separated by AND are TRUE.
• The OR operator displays a record if any of the conditions separated by OR is TRUE.
It is a SQL statement use to select records when both condition are true or one condition is true.
It is a SQL statement use to select records either one condition must be true then output will be
true.
The following SQL statement selects all fields from "SignUp" where FN is "Rahul" OR LN is
"Gandhi":
Syntax
Example
Syntax
Example
SELECT Email Id
FROM SignUp
WHERE FN=’Rahul’ OR LN=’Gandhi’;
Output
Email Id
1 [email protected]
2 [email protected]
2. AND
It is a SQL statement in that select record when both the condition must be true then output will
be true.
The following SQL statement selects all fields from "SignUp" where FN is "Rahul" AND LN is
"Gandhi":
Syntax
Example
Output
Syntax
Example
SELECT Mob. No
FROM SignUp
WHERE FN=’Rahul’ AND LN=’Gandhi’;
Output
Mob. No
1 1111
6. 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 following SQL statement selects all SignUp with a FN starting with "A":
Syntax
Example
Output
SELECT column1,column2,...
FROM table_name
WHERE column LIKE pattern;
Example
SELECT LN
FROM SignUp
WHERE FN LIKE “A%”;
Output
LN
1 Kejriwal
2 Hajare
The following SQL statement selects all SignUp with a FN ending with "d":
Syntax
Example
Syntax
SELECT column1,column2,...
FROM table_name
WHERE column LIKE pattern;
Example
SELECT LN
FROM SignUp
WHERE FN LIKE “%d”;
Output
LN
1 Kejriwal
2 Pawar
Select all records where the value of the FN column does NOT start with the letter "A".
Syntax
Output
Select all records where the value of the column starts with the letter "A".
Syntax
Select all records where the value of the column ends with the letter "a".
Syntax
Select all records where the value of the column starts with letter "A" and ends with the
letter "b".
Syntax
Syntax
Select all records where the value of the column does NOT start with the letter "A".
Syntax
The following SQL statement selects all Sign Up with a FN starting with "A", "B", or "C":
Syntax
The following SQL statement selects all Sign Up with a FN ending with "A", "B", or "C":
Syntax
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 3 characters in length
WHERE Column LIKE 'a%o' Finds any values that start with "a" and ends
with "o"
7. WILDCARD
Wildcard characters are used with the LIKE operator. The LIKE operator is used in
a WHERE clause to search for a specified pattern in a column.
The following SQL statement selects all SignUp with a FN starting with any character, followed
by "ahul":
Syntax
Example
Output
The following SQL statement selects all SignUp that are Present in "Rahul", "Soniya":
Syntax
Example
Output
Syntax
Example
Output
The BETWEEN operator selects values within a given range. The values can be numbers,
text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
Syntax
Example
Output
Output
Example
Output
To display the products outside the range of the previous example, use NOT BETWEEN:
Syntax
Example
Output
2. If you are adding values for all the columns of the table, you do not need to specify the column
names in the SQL query. However, make sure the order of the values is in the same order as the
columns in the table. Here, the INSERT INTO syntax would be as follows:
The following SQL statement inserts a new record in the "SignUp" table:
Syntax
Example
The following SQL statement will insert a new record, but only insert data in the "FN", "LN",
and "MobNo" columns (Sr.No will be updated automatically):
Syntax
Example
Output
Did you notice that we did not insert any number into the Sr.No field?
The Sr.No column is in auto-increment field and will be generated automatically when a new
record is inserted in to the table.
11. UPDATE
The following SQL statement updates the FN with a new Mob.No, Email Id & City.
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example
UPDATE SignUp
SET Mob.No = 0000, Email Id = [email protected], City = Delhi
WHERE FN = Rahul;
Output
Update Warning!
Be careful when updating records. If you omit the WHERE clause, ALL records will be updated!
Example
UPDATE SignUp
SET Mob.No = 0000, Email Id = [email protected], City = Delhi
Output
Note: Be careful when updating records in a table! Notice the WHERE clause in
the UPDATE statement. The WHERE clause specifies which record(s) that should be updated. If
you omit the WHERE clause, all records in the table will be updated!
12. DELETE
The following SQL statement deletes the Mob.No "2222" from the "SignUp" table:
Syntax
Example
Output
Note: Be careful when deleting records in a table! Notice the WHERE clause in
the DELETE statement. The WHERE clause specifies which record(s) should be deleted. If you
omit the WHERE clause, all records in the table will be deleted!
It is possible to delete all rows in a table without deleting the table. This means that the table
structure, attributes, and indexes will be intact:
Syntax
The following SQL statement deletes all rows in the "SignUp" table, without deleting the table:
Example
Output
13. SELECT IN TO
The SELECT INTO statement copies data from one table into a new table.
Syntax
Example
Output
It is a SQL statement it is used for to change the temporary column name & table name without
changing in database.
SQL aliases are used to give a table, or a column in a table, a temporary name.
SELECT column_name(s)
FROM table_name AS alias_name;
The following SQL statement creates two aliases, one for the FN column and one for the LN
column:
Example
Example
Output
15. UNION
The UNION operator is used to combine the result-set of two or more SELECT statements.
• Every SELECT statement within UNION must have the same number of columns
• The columns must also have similar data types
• The columns in every SELECT statement must also be in the same order
UNION Syntax
Example
Example
Output
LN
1 Deshmukh
2 Gandhi
3 Hajare
4 Kejriwal
5 Pawar
6 Raut
7 Thakre
Example
SELECT Seats FROM SignUp
UNION
SELECT Seats FROM Register;
Output
Seats
1 45
2 49
3 53
4 54
5 55
6 56
7 57
UNION ALL
The UNION operator selects only distinct values by default. To allow duplicate values,
use UNION ALL:
Syntax
Example
LN
1 Gandhi
2 Kejriwal
3 Hajare
4 Pawar
5 Gandhi
6 Gandhi
7 Kejriwal
8 Hajare
9 Pawar
10 Gandhi
11 Raut
12 Thakre
13 Pawar
14 Deshmukh
16. CREATE TABLE
Syntax
(
column1 datatype,
column2 datatype,
column3 datatype,
....
);
The column parameters specify the names of the columns of the table.
The data type parameter specifies the type of data the column can hold (e.g. varchar, integer,
date, etc.).
Example
The following example creates a table called "SignUp" that contains six columns: ID, FN, LN,
Mob.No, Email Id and City:
(
ID int,
FN varchar(255),
LN varchar(255),
Mob.No int,
Email Id varchar(255),
City varchar(255)
);
The ID, Mob.No, column is of type int and will hold an integer.
The FN, LN, Email Id and City columns are of type varchar and will hold characters, and the
maximum length for these fields is 255 characters.
The empty "Sign Up" table will now look like this:
Output
17. DROP
It is used to delete both the structure & records stored in the table
Syntax
Example
Output
Note: Be careful before dropping a table. Deleting a table will result in loss of complete
information stored in the table!
18. TRUNCATE
The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table
itself.
It is used to delete all the rows from the table means to records stored in the table & the structure
remain same. Not delete the structure of the table.
Syntax
Example
Output
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.
Syntax
Example
Output
To delete a column in a table, use the following syntax (notice that some database systems don't
allow deleting a column):
The following SQL deletes the "Address" column from the "SignUp" table:
Syntax
Example
Output
To change the data type of a column in a table, use the following syntax:
Example
Notice that the "Date of Birth" column is now of data type year and is going to hold a year in a
two- or four-digit format.
Output
"Date of Birth" column is now of data type year and is going to hold a year in a two- or four-
digit format.
20. CONSTRAINTS
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.
This ensures the accuracy and reliability of the data in the table. If there is any violation between
the constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and
table level constraints apply to the whole table.
Syntax
CREATE TABLE T.N.
(
C.N.1 datatype constraint,
C.N 2 datatype constraint,
datatype constraint, ....
);
Example
CREATE TABLE VCTC
(
Sr No int Primary Key,
Fname varchar(255) Not Null,
Lname varchar(255) Not Null,
Mockresult int Check (Mockresult < 10),
Location varchar(255) Default “Punevctc”
Mobileno int Unique
);
The following constraints are commonly used in SQL:
1. NOTNULL
The NOT NULL constraint enforces a column to NOT accept NULL values.
Syntax
(
column1 datatype NOT NULL,
column2 datatype NOT NULL,
column3 datatype NOT NULL,
....
);
The following SQL ensures that the "ID", "FN", "LN", “Mob. No”, “Email Id” and “City”
columns will NOT accept NULL values when the "SignUp" table is created:
Example
(
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
);
2. UNIQUE KEY
Each table can have more than one / multiple Unique Constraint / value.
Syntax
(
column1 datatype NOT NULL UNIQUE,
column2 datatype NULL,
column3 datatype NOT NULL,
Column4 datatype NOT NULL UNIQUE,
....
);
The following SQL creates a UNIQUE constraint on the "ID" & “Mob. No” column when the
"SignUp" table is created:
Example
(
ID int NOT NULL/UNIQUE,
FN varchar(255) NOT NULL,
LN varchar(255) NOT NULL,
Mob.No int NOT NULL/ UNIQUE,
Email Id varchar(255) NOT NULL,
City varchar(255) NOT NULL
);
3. PRIMARY KEY
The PRIMARY KEY constraint uniquely identifies each record (rows) in a table.
A combination of a NOT NULL and UNIQUE. Uniquely identifies each row 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).
Syntax
(
column1 datatype NOT NULL UNIQUE,
column2 datatype NOT NULL,
column3 datatype NOT NULL,
column4 datatype NOT NULL UNIQUE,
PRIMARY KEY (column)
....
);
The following SQL creates a PRIMARY KEY on the "ID" column when the "SignUp" table is
created:
Example
(
ID int NOT NULL UNIQUE,
FN varchar(255) NOT NULL,
LN varchar(255) NOT NULL,
Mob.No int NOT NULL/ UNIQUE,
Email Id varchar(255) NOT NULL,
City varchar(255) NOT NULL,
PRIMARY KEY (ID)
);
4. FORGIEN 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.
Syntax
(
column1 datatype NOT NULL/NULL,
column2 datatype NOT NULL/NULL,
column3 datatype NOT NULL/NULL,
column4 datatype NOT NULL/NULL,
FOREIGN KEY (column)
....
);
Example
AADHAR NO int(25),
LASTNAME varchar(256),
CITY varchar(25),
FIRSTNAME varchar(256),
LASTNAME varchar(256),
)
CREATE TABLE IDBI
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.
5. DEFAULT
The following SQL sets a DEFAULT value for the "City" column when the "Persons" table is
created:
Syntax
(
column1 datatype NOT NULL/NULL,
column2 datatype NOT NULL/NULL,
column3 datatype NOT NULL/NULL DEFAULT “Enter Name/City”,
column4 datatype NOT NULL/NULL,
....
);
Example
(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int NOT NULL,
City varchar(255) DEFAULT 'Latur'
);
Output
The CHECK constraint is used to limit the value range that can be placed in a column.
If you define a CHECK constraint on a column it will allow only certain values for this column.
If you define a CHECK constraint on a table it can limit the values in certain columns based on
values in other columns in the row.
Syntax
(
column1 datatype NOT NULL/NULL,
column2 datatype NOT NULL/NULL,
column3 datatype NOT NULL/NULL,
column4 datatype NOT NULL/NULL,
CHECK (column) <, >, = Value
....
);
Example
(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int NOT NULL,
CHECK (Age>21)
);
Output
2 23
Joins are statements use to joint two or more tables on primary key & foregin key
Join is an SQL statement it is used to join the two or more tables based on the basic of
relationship between the primary key & foregin key
Types of JOIN
1. INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
Inner join is a SQL statement which is use to show records in columns which are related to
common values
Syntax
SELECT column_name(s)
FROM table1 INNER JOIN table2
ON table1.column_name = table2.column_name;
or
SELECT Table1.Column1
Table1.Column2
Table2.Column1
FROM Table1 INNER JOIN Table2
ON Table1.PK = Table2.FK;
Example
Table1- UIDAI
Aadhar (PK) First Name Last Name City
1 Sharad Pawar Baramati
2 Ajit Pawar Pune
3 Sanjay Raut Mumbai
Table2- IDBI
Account No Balance Aadhar(FK)
1234 50000 2
5898 40000 3
9872 30000 2
8796 90000 5
Example
SELECT UIDAI. First Name
UIDAI. Last Name
IDBI. Balance
FROM UIDAI INNER JOIN IDBI
ON UIDAI. Aadhar = IDBI. Aadhar;
Output
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.
Syntax
SELECT Table1.Column1
Table1.Column1
Table2.Column1
FROM Table1 LEFT JOIN Table2
ON Table1.PK = Table2.FK;
Example
3. 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 Table1.Column1
Table1.Column1
Table2.Column1
FROM Table1 RIGHT JOIN Table2
ON Table1.PK = Table2.FK;
Example
Output
4. FULL JOIN
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or
right (table2) table records.
Tip: FULL OUTER JOIN and FULL JOIN are the same.
Syntax
SELECT Table1.Column1
Table1.Column2
Table2.Column1
FROM Table1 FULL JOIN Table2
ON Table1.PK = Table2.FK;
Example
Output
The GROUP BY statement group’s rows that have the same values into summary rows.
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.
Account No Withdrawal
1 6000
2 7000
1 7000
3 6000
1 2000
2 4000
3 9000
4 3000
Syntax
Example
Withdrawal
15000
11000
15000
3000
Syntax
SELECT Column, AGGREGATE FUNCTIONS (Column) FROM table_name
GROUP BY Column;
Example
SELECT Account No., SUM (Withdrawal) FROM ICICI
GROUP BY Account No.;
Output
Account No Withdrawal
1 15000
2 11000
3 15000
4 3000
23. HAVING
It is a SQL statement use to satisfy the given condition & it is use with the aggregate
function.
The HAVING clause was added to SQL because the WHERE keyword cannot be used
with aggregate functions.
Syntax
Example
Output
Withdrawal
11000
3000
Syntax
SELECT Column, AGGREGATE FUNCTIONS (Column) FROM table_name
GROUP BY Column;
HAVING AGGREGATE FUNCTIONS (Column) >, <, =, Value;
Example
SELECT Account No., SUM (Withdrawal) FROM ICICI
GROUP BY Account No.;
HAVING SUM (Withdrawal) <= 12000;
Output
Account No Withdrawal
2 11000
4 3000
24. AGGREGATE FUNCTIONS
-An aggregate function performs calculations on set of values and returns a single value.
-To perform calculation on multiple rows of a single column and return single value.
Table-Students Marks
Sr.No FN LN Marks
1 Virat Kohli 65
2 Rohit Sharma 75
3 Ravindra Jadeja 95
4 Shikhar Dhawan 85
5 Rishabh Pant 55
FIRST():
- Will returns first value in the selected column except NULL
- Returns first value of a numeric column
Syntax:
SELECT FIRST(column_name)
FROM table_name;
Example:
SELECT FIRST(Marks)
FROM Student Marks;
Output
FIRST(Marks)
1 65
LAST():
- Will returns last value in the selected column except NULL
- Returns last value of a numeric column
Syntax:
SELECT LAST(column_name)
FROM table_name;
Example:
SELECT LAST(Marks)
FROM Student Marks;
Output:
LAST(Marks)
1 55
MIN():
- Will returns minimum value in the selected column except NULL
- Returns minimum value of a numeric column
Syntax:
SELECT MIN(column_name)
FROM table_name;
Example:
SELECT MIN(Marks)
FROM Student Marks;
Output:
MIN(Marks)
1 55
MAX():
Syntax:
SELECT MAX(column_name)
FROM table_name;
Example:
SELECT MAX(Marks)
FROM Student Marks;
Output:
MAX(Marks)
1 95
SUM():
Syntax:
SELECT SUM(column_name)
FROM table_name;
Example:
SELECT SUM(Marks)
FROM Student Marks;
Output:
SUM(Marks)
1 375
AVG():
- This function returns average value of a numeric column
- It will apply for Non-Null Values
- AVG=SUM(column_name)/COUNT(column_name);
Syntax:
SELECT AVG(column_name)
FROM table_name;
Example:
SELECT AVG(Marks)
FROM Student Marks;
Output:
AVG(Marks)
1 75
COUNT():
- Returns total number of records
- Return Non-Null Values of a column
Syntax:
SELECT COUNT(column_name)
FROM table_name;
Example:
SELECT COUNT(Marks)
FROM Student Marks;
Output:
COUNT(Marks)
1 5
Find the highest Salary
Table Name – Employ Info
Id. No FN LN Salary
1 Virat Kohli 1000
2 Rohit Sharma 2000
3 Ravindra Jadeja 3000
4 Shikhar Dhawan 4000
5 Rishabh Pant 5000
6 Suryakumar Yadav 6000
7 Ishan Kishan 7000
8 Hardik Pandya 8000
9 Jasprit Bumrah 9000
10 Bhuvneshwar Kumar 10000
To show 2nd max salary / How to get the 2nd max salary
• Syntax
• Example
• Syntax
• Example
• Syntax
To show 3rd max salary / How to get the 3rd max salary
• Syntax
Or
SELECT TOP 1 Salary FROM (SELECT TOP 3 Salary FROM Employ Info
To show 7th highest salary / How to get the 7th highest salary
• Syntax
• Example
• Syntax
ORDER BY Column);
• Example
ORDER BY Salary
By using RONUM to find highest & lowest salary
//Highest salary
• Syntax
• Example
//Lowest Salary
• Syntax
• Example
• Syntax
• Example
• Syntax
• Example
• Syntax
SELECT*FROM (SELECT*FROM (SELECT*FROM (SELECT*FROM table_name
ORDER BY Column DESC) WHERE ROWNUM<=N) ORDER BY Column) WHERE
ROWNUM<=Value;
Execution
WHERE ROWNUM<=3;
ORDER BY Salary;
WHERE ROWNUM<=1;
//Nth Lowest Salary
• Syntax
SELECT*FROM (SELECT*FROM (SELECT*FROM (SELECT*FROM table_name
ORDER BY Column) WHERE ROWNUM<=N) ORDER BY Column DESC) WHERE
ROWNUM<=Value;
Execution
ORDER BY Salary;
WHERE ROWNUM<=3;
WHERE ROWNUM<=1;
SQL Questions