You are on page 1of 35

--3rd Highest Salary

select min(Emp_Sal) from Employee_Test where Emp_Sal in


(select distinct top 3 Emp_Sal from Employee_Test order by Emp_Sal desc)
--nth Highest Salary
select min(Emp_Sal) from Employee_Test where Emp_Sal in
(select distinct top n Emp_Sal from Employee_Test order by Emp_Sal desc)

Deleting duplicate rows from a table


A table with a primary key doesnt contain duplicates. But if due to some reason, the keys have to be
disabled or when importing data from other sources, duplicates come up in the table data, it is often
needed to get rid of such duplicates.
This can be achieved in tow ways :(a) Using a temporary table.
(b) Without using a temporary table.

select top 0* into temp from employee


insert into temp select id,name,salary from employee groupby
id,name,salary;
truncate table employee
insert into employee select * from temp;

(b) Without using a temporary table


Hide Copy Code

;with T as
(
select * , row_number() over (partition by Emp_ID order by Emp_ID) as rank
from employee_test1
)
delete
from T
where rank > 1

The result is as:Hide Copy Code

Emp_ID
1
2
3
4
5
6

Emp_name
Anees
Rick
John
Stephen
Maria
Tim

Emp_Sal
1000
1200
1100
1300
1400
1150

Add Unique Index on your table:


ALTER IGNORE TABLE `TableA`
ADD UNIQUE INDEX (`member_id`, `quiz_num`, `question_num`, `answer_num`);

OR
Add primry key in your table then you can easily remove duplicates from your table using below query:
DELETE FROM member
WHERE id IN (SELECT *
FROM (SELECT id FROM member
GROUP BY member_id, quiz_num, question_num, answer_num HAVING (COUNT(*) > 1)
) AS A
);

If you can afford to rewrite the whole table, this is probably the simplest approach:
WITH Deleted AS (
DELETE FROM discogs.releases_labels
RETURNING *
)
INSERT INTO discogs.releases_labels
SELECT DISTINCT * FROM Deleted

If you need to specifically target the duplicated records, you can make use of the internal ctid field, which
uniquely identifies a row:
DELETE FROM discogs.releases_labels
WHERE ctid NOT IN (
SELECT MIN(ctid)
FROM discogs.releases_labels
GROUP BY label, release_id, catno
)

Be very careful with ctid; it changes over time. But you can rely on it staying the same within the scope of
a single statement.

Use the ON DELETE CASCADE option if you want rows deleted in the child table when corresponding
rows are deleted in the parent table. If you do not specify cascading deletes, the default behavior of
the database server prevents you from deleting data in a table if other tables reference it.
ALTER TABLE [dbo].[ProductDetails] WITH CHECK ADD CONSTRAINT
[FK_ProductDetails_Products] FOREIGN KEY([ProductID])
REFERENCES [dbo].[Products] ([ProductID])
ON UPDATE CASCADE
ON DELETE CASCADE

1. SQL Query to find second highest salary of Employee


This SQL query can also be solved using following subquery-

select MAX(Salary) from Employee WHERE Salary NOT IN (select MAX(Salary) from Employee);
2. SQL Query to find Max Salary from each department
You can find maximum salary for each department by grouping all records using DeptID and
then use MAX function to calculate maximum salary in each group or department.
SELECT DeptID, MAX(Salary) FROM Employee GROUP BY DeptID.
3. Write SQL Query to display current date
SELECT GetDate();
4. Write an SQL Query to check whether date passed to Query is date of given format
or not
SELECT ISDATE('1/08/13') AS "MM/DD/YY";
5. Write a SQL Query to print the name of distinct employee whose DOB is between
01/01/1960 to 31/12/1975
SELECT DISTINCT EmpName FROM Employees WHERE DOB BETWEEN 01/01/1960 AND
31/12/1975;
6. Write an SQL Query find number of employees according to gender whose DOB is
between 01/01/1960 to 31/12/1975
SELECT COUNT(*), sex from Employees WHERE DOB BETWEEN '01/01/1960' AND '31/12/1975'
GROUP BY sex;
7. Write an SQL Query to find employee whose Salary is equal or greater than 10000
SELECT EmpName FROM Employees WHERE Salary>=10000;
8. Write a SQL Query to find year from date
You can use the following query to find year from a date in SQL server 2008.
SELECT YEAR(GETDATE()) as "Year";
9. Write SQL Query to find duplicate rows in a database? and then write SQL query to

delete them?
You can use simple query like this to select specific date in distinct record
SELECT * FROM emp a WHERE rowid = (SELECT MAX(rowid) FROM EMP b WHERE
a.empno=b.empno)
To delete
DELETE FROM emp a WHERE rowid != (SELECT MAX(rowid) FROM emp b WHERE
a.empno=b.empno);
10. There is a table which contains two column Student and Marks, you need to find all
the students, whose marks are greater than average marks i.e. list of above average
students
SELECT student, marks from table where marks > SELECT AVG(marks) from table)

Normalization of Database
Database Normalisation is a technique of organizing the data in the database. Normalization is a systematic
approach of decomposing tables to eliminate data redundancy and undesirable characteristics like Insertion,
Update and Deletion Anamolies.
Normalization is used for mainly two purpose,

Eliminating reduntant(useless) data.

Ensuring data dependencies make sense i.e data is logically stored.

Updation Anamoly : To update address of a student who occurs twice or more than twice in a table, we will
have to update S_Address column in all the rows, else data will become inconsistent.

Insertion Anamoly : Suppose for a new admission, we have a Student id(S_id), name and address of a
student but if student has not opted for any subjects yet then we have to insert NULL there, leading to
Insertion Anamoly.

Deletion Anamoly : If (S_id) 401 has only one subject and temporarily he drops it, when we delete that row,
entire student record will be deleted along with it.

First Normal Form (1NF)


As per First Normal Form, no two Rows of data must contain repeating group of information i.e each set of column
must have a unique value, such that multiple columns cannot be used to fetch the same row. Each table should be
organized into rows, and each row should have a primary key that distinguishes it as unique.
The Primary key is usually a single column, but sometimes more than one column can be combined to create a
single primary key. For example consider a table which is not in First normal form
Student Table :
Student

Age

Subject

Adam

15

Biology, Maths

Alex

14

Maths

Stuart

17

Maths

In First Normal Form, any row must not have a column in which more than one value is saved, like separated with
commas. Rather than that, we must separate such data into multiple rows.
Student Table following 1NF will be :
Student

Age

Subject

Adam

15

Biology

Adam

15

Maths

Alex

14

Maths

Stuart

17

Maths

Using the First Normal Form, data redundancy increases, as there will be many columns with same data in multiple
rows but each row as a whole will be unique.

Second Normal Form (2NF)


As per the Second Normal Form there must not be any partial dependency of any column on primary key. It means
that for a table that has concatenated primary key, each column in the table that is not part of the primary key must
depend upon the entire concatenated key for its existence. If any column depends only on one part of the
concatenated key, then the table fails Second normal form.
In example of First Normal Form there are two rows for Adam, to include multiple subjects that he has opted for.
While this is searchable, and follows First normal form, it is an inefficient use of space. Also in the above Table in
First Normal Form, while the candidate key is {Student, Subject}, Age of Student only depends on Student column,
which is incorrect as per Second Normal Form. To achieve second normal form, it would be helpful to split out the
subjects into an independent table, and match them up using the student names as foreign keys.
New Student Table following 2NF will be :
Student

Age

Adam

15

Alex

14

Stuart

17

In Student Table the candidate key will be Student column, because all other column i.e Age is dependent on it.
New Subject Table introduced for 2NF will be :
Student

Subject

Adam

Biology

Adam

Maths

Alex

Maths

Stuart

Maths

In Subject Table the candidate key will be {Student, Subject} column. Now, both the above tables qualifies for
Second Normal Form and will never suffer from Update Anomalies. Although there are a few complex cases in
which table in Second Normal Form suffers Update Anomalies, and to handle those scenarios Third Normal Form is
there.

Third Normal Form (3NF)


Third Normal form applies that every non-prime attribute of table must be dependent on primary key, or we can
say that, there should not be the case that a non-prime attribute is determined by another non-prime attribute. So
this transitive functional dependency should be removed from the table and also the table must be in Second
Normal form. For example, consider a table with following fields.
Student_Detail Table :
Student_id

Student_name

DOB

Street

city

State

Zip

In this table Student_id is Primary key, but street, city and state depends upon Zip. The dependency between zip
and other fields is called transitive dependency. Hence to apply 3NF, we need to move the street, city and state to
new table, with Zip as primary key.
New Student_Detail Table :
Student_id

Student_name

DOB

Address Table :
Zip

Street

The advantage of removing transtive dependency is,

Amount of data duplication is reduced.

city

state

Zip

Data integrity achieved.

Boyce and Codd Normal Form (BCNF)


Boyce and Codd Normal Form is a higher version of the Third Normal form. This form deals with certain type of
anamoly that is not handled by 3NF. A 3NF table which does not have multiple overlapping candidate keys is said to
be in BCNF. For a table to be in BCNF, following conditions must be satisfied:

R must be in 3rd Normal Form

and, for each functional dependency ( X -> Y ), X should be a super Key.

First
Normal Form
First Normal Form is defined in the definition of relations (tables) itself. This rule defines
that all the attributes in a relation must have atomic domains. The values in an atomic
domain are indivisible units.

We re-arrange the relation (table) as below, to convert it to First Normal Form.

Each attribute must contain only a single value from its pre-defined domain.

Second Normal Form


Before we learn about the second normal form, we need to understand the following

Prime attribute An attribute, which is a part of the prime-key, is known as a prime


attribute.

Non-prime attribute An attribute, which is not a part of the prime-key, is said to be a


non-prime attribute.

If we follow second normal form, then every non-prime attribute should be fully
functionally dependent on prime key attribute. That is, if X A holds, then there
should not be any proper subset Y of X, for which Y A also holds true.

We see here in Student_Project relation that the prime key attributes are Stu_ID and
Proj_ID. According to the rule, non-key attributes, i.e. Stu_Name and Proj_Name must
be dependent upon both and not on any of the prime key attribute individually. But we
find that Stu_Name can be identified by Stu_ID and Proj_Name can be identified by
Proj_ID independently. This is calledpartial dependency, which is not allowed in
Second Normal Form.

We broke the relation in two as depicted in the above picture. So there exists no partial
dependency.

Third Normal Form


For a relation to be in Third Normal Form, it must be in Second Normal form and the
following must satisfy

No non-prime attribute is transitively dependent on prime key attribute.

For any non-trivial functional dependency, X A, then either


o

X is a superkey or,

A is prime attribute.

We find that in the above Student_detail relation, Stu_ID is the key and only prime key
attribute. We find that City can be identified by Stu_ID as well as Zip itself. Neither Zip
is a superkey nor is City a prime attribute. Additionally, Stu_ID Zip City, so there
exists transitive dependency.

To bring this relation into third normal form, we break the relation into two relations as
follows

Boyce-Codd Normal Form


Boyce-Codd Normal Form (BCNF) is an extension of Third Normal Form on strict terms.
BCNF states that

For any non-trivial functional dependency, X A, X must be a super-key.

In the above image, Stu_ID is the super-key in the relation Student_Detail and Zip is
the super-key in the relation ZipCodes. So,
Stu_ID Stu_Name, Zip
and
Zip City
Which confirms that both the relations are in BCNF.

Join in SQL
SQL Join is used to fetch data from two or more tables, which is joined to appear as single set of data. SQL Join is
used for combining column from two or more tables by using values common to both tables. JoinKeyword is used
in SQL queries for joining two or more tables. Minimum required condition for joining table, is(n-1) where n, is
number of tables. A table can also join to itself known as, Self Join.

Types of Join
The following are the types of JOIN that we can use in SQL.

Inner

Outer

Left

Right

Cross JOIN or Cartesian Product


This type of JOIN returns the cartesian product of rows of from the tables in Join. It will return a table which consists
of records which combines each row from the first table with each row of the second table.
Cross JOIN Syntax is,
SELECT column-name-list
from table-name1
CROSS JOIN
table-name2;

Example of Cross JOIN


The class table,
ID

NAME

abhi

adam

alex

The class_info table,


ID

Address

DELHI

MUMBAI

CHENNAI

Cross JOIN query will be,


SELECT *
from class,
cross JOIN class_info;
The result table will look like,
ID

NAME

ID

Address

abhi

DELHI

adam

DELHI

alex

DELHI

abhi

MUMBAI

adam

MUMBAI

alex

MUMBAI

abhi

CHENNAI

adam

CHENNAI

alex

CHENNAI

INNER Join or EQUI Join


This is a simple JOIN in which the result is based on matched data as per the equality condition specified in the
query.
Inner Join Syntax is,
SELECT column-name-list
from table-name1
INNER JOIN
table-name2
WHERE table-name1.column-name = table-name2.column-name;

Example of Inner JOIN


The class table,
ID

NAME

abhi

adam

alex

anu

The class_info table,

ID

Address

DELHI

MUMBAI

CHENNAI

Inner JOIN query will be,


SELECT * from class, class_info where class.id = class_info.id;
The result table will look like,
ID

NAME

ID

Address

abhi

DELHI

adam

MUMBAI

alex

CHENNAI

Natural JOIN
Natural Join is a type of Inner join which is based on column having same name and same datatype present in both
the tables to be joined.
Natural Join Syntax is,
SELECT *
from table-name1
NATURAL JOIN
table-name2;

Example of Natural JOIN


The class table,
ID

NAME

abhi

adam

alex

anu

The class_info table,


ID

Address

DELHI

MUMBAI

CHENNAI

Natural join query will be,


SELECT * from class NATURAL JOIN class_info;
The result table will look like,
ID

NAME

Address

abhi

DELHI

adam

MUMBAI

alex

CHENNAI

In the above example, both the tables being joined have ID column(same name and same datatype), hence the
records for which value of ID matches in both the tables will be the result of Natural Join of these two tables.

Outer JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,

Left Outer Join

Right Outer Join

Full Outer Join

Left Outer Join


The left outer join returns a result table with the matched data of two tables then remaining rows of the lefttable
and null for the right table's column.
Left Outer Join syntax is,
SELECT column-name-list
from table-name1
LEFT OUTER JOIN
table-name2
on table-name1.column-name = table-name2.column-name;
Left outer Join Syntax for Oracle is,
select column-name-list
from table-name1,
table-name2
on table-name1.column-name = table-name2.column-name(+);

Example of Left Outer Join


The class table,
ID

NAME

abhi

adam

alex

anu

ashish

The class_info table,


ID

Address

DELHI

MUMBAI

CHENNAI

NOIDA

PANIPAT

Left Outer Join query will be,


SELECT * FROM class LEFT OUTER JOIN class_info ON (class.id=class_info.id);
The result table will look like,

ID

NAME

ID

Address

abhi

DELHI

adam

MUMBAI

alex

CHENNAI

anu

null

null

ashish

null

null

Right Outer Join


The right outer join returns a result table with the matched data of two tables then remaining rows of the right
table and null for the left table's columns.
Right Outer Join Syntax is,
select column-name-list
from table-name1
RIGHT OUTER JOIN
table-name2
on table-name1.column-name = table-name2.column-name;
Right outer Join Syntax for Oracle is,
select column-name-list
from table-name1,
table-name2
on table-name1.column-name(+) = table-name2.column-name;

Example of Right Outer Join


The class table,
ID

NAME

abhi

adam

alex

anu

ashish

The class_info table,


ID

Address

DELHI

MUMBAI

CHENNAI

NOIDA

PANIPAT

Right Outer Join query will be,


SELECT * FROM class RIGHT OUTER JOIN class_info on (class.id=class_info.id);
The result table will look like,

ID

NAME

ID

Address

abhi

DELHI

adam

MUMBAI

alex

CHENNAI

null

null

NOIDA

null

null

PANIPAT

Full Outer Join


The full outer join returns a result table with the matched data of two table then remaining rows of both lefttable
and then the right table.
Full Outer Join Syntax is,
select column-name-list
from table-name1
FULL OUTER JOIN
table-name2
on table-name1.column-name = table-name2.column-name;

Example of Full outer join is,


The class table,
ID

NAME

abhi

adam

alex

anu

ashish

The class_info table,


ID

Address

DELHI

MUMBAI

CHENNAI

NOIDA

PANIPAT

Full Outer Join query will be like,


SELECT * FROM class FULL OUTER JOIN class_info on (class.id=class_info.id);
The result table will look like,
ID

NAME

ID

Address

abhi

DELHI

adam

MUMBAI

alex

CHENNAI

anu

null

null

ashish

null

null

null

null

NOIDA

null

null

PANIPAT

SQL Constraints
SQl Constraints are rules used to limit the type of data that can go into a table, to maintain the accuracy and
integrity of the data inside table.
Constraints can be divided into following two types,

Column level constraints : limits only column data

Table level constraints : limits whole table data

Constraints are used to make sure that the integrity of data is maintained in the database. Following are the most
used constraints that can be applied to a table.

NOT NULL

UNIQUE

PRIMARY KEY

FOREIGN KEY

CHECK

DEFAULT

Order By Clause
Order by clause is used with Select statement for arranging retrieved data in sorted order. The Order byclause by
default sort data in ascending order. To sort data in descending order DESC keyword is used withOrder by clause.

Syntax of Order By
SELECT column-list|* from table-name order by asc|desc;

Distinct keyword
The distinct keyword is used with Select statement to retrieve unique values from the table. Distinctremoves all
the duplicate records while retrieving from database.

Syntax for DISTINCT Keyword


SELECT distinct column-name from table-name;

HAVING Clause
having clause is used with SQL Queries to give more precise condition for a statement. It is used to mention
condition in Group based SQL functions, just like WHERE clause.
Syntax for having will be,
select column_name, function(column_name)
FROM table_name
WHERE column_name condition
GROUP BY column_name
HAVING function(column_name) condition

Like clause
Like clause is used as condition in SQL query. Like clause compares data with an expression using wildcard
operators. It is used to find similar data from the table.

Wildcard operators
There are two wildcard operators that are used in like clause.

Percent sign % : represents zero, one or more than one character.

Underscore sign _ : represents only one character.

TCL command
Transaction Control Language(TCL) commands are used to manage transactions in database.These are used to
manage the changes made by DML statements. It also allows statements to be grouped together into logical
transactions.

Commit command
Commit command is used to permanently save any transaaction into database.
Following is Commit command's syntax,
commit;

Rollback command
This command restores the database to last commited state. It is also use with savepoint command to jump to a
savepoint in a transaction.
Following is Rollback command's syntax,
rollback to savepoint-name;

Savepoint command
savepoint command is used to temporarily save a transaction so that you can rollback to that point whenever
necessary.
Following is savepoint command's syntax,
savepoint savepoint-name;

Example of Savepoint and Rollback


Following is the class table,
ID

NAME

abhi

adam

alex

Lets use some SQL queries on the above table and see the results.
INSERT into class values(5,'Rahul');
commit;
UPDATE class set name='abhijit' where id='5';
savepoint A;
INSERT into class values(6,'Chris');
savepoint B;
INSERT into class values(7,'Bravo');
savepoint C;
SELECT * from class;

DML command
Data Manipulation Language (DML) statements are used for managing data in database. DML commands are not
auto-committed. It means changes made by DML command are not permanent to database, it can be rolled back.

1) INSERT command
Insert command is used to insert data into a table. Following is its general syntax,
INSERT into table-name values(data1,data2,..)

2) UPDATE command
Update command is used to update a row of a table. Following is its general syntax,
UPDATE table-name set column-name = value where condition;

3) Delete command
Delete command is used to delete data from a table. Delete command can also be used with condition to delete a
particular row. Following is its general syntax,
DELETE from table-name;

SQL queries to Truncate, Drop or Rename a Table


truncate command
truncate command removes all records from a table. But this command will not destroy the table's structure. When
we apply truncate command on a table its Primary key is initialized. Following is its Syntax,
truncate table table-name
Here is an Example explaining it.
truncate table Student;
The above query will delete all the records of Student table.
truncate command is different from delete command. delete command will delete all the rows from a table whereas
truncate command re-initializes a table(like a newly created table).
For eg. If you have a table with 10 rows and an auto_increment primary key, if you use delete command to delete
all the rows, it will delete all the rows, but will not initialize the primary key, hence if you will insert any row after
using delete command, the auto_increment primary key will start from 11. But in case of truncatecommand, primary
key is re-initialized.

drop command
drop query completely removes a table from database. This command will also destroy the table structure.
Following is its Syntax,
drop table table-name
Here is an Example explaining it.

drop table Student;


The above query will delete the Student table completely. It can also be used on Databases. For Example, to drop
a database,
drop database Test;
The above query will drop a database named Test from the system.

rename query
rename command is used to rename a table. Following is its Syntax,
rename table old-table-name to new-table-name
Here is an Example explaining it.
rename table Student to Student-record;
The above query will rename Student table to Student-record.

alter command
alter command is used for alteration of table structures. There are various uses of alter command, such as,

to add a column to existing table

to rename any existing column

to change datatype of any column or to modify its size.

alter is also used to drop a column.

To Add Column to existing Table


Using alter command we can add a column to an existing table. Following is the Syntax,
alter table table-name add(column-name datatype);
Here is an Example for this,

alter table Student add(address char);


The above command will add a new column address to the Student table

To Add Multiple Column to existing Table


Using alter command we can even add multiple columns to an existing table. Following is the Syntax,
alter table table-name add(column-name1 datatype1, column-name2 datatype2, column-name3
datatype3);
Here is an Example for this,
alter table Student add(father-name varchar(60), mother-name varchar(60), dob date);
The above command will add three new columns to the Student table

To Add column with Default Value


alter command can add a new column to an existing table with default values. Following is the Syntax,
alter table table-name add(column-name1 datatype1 default data);
Here is an Example for this,
alter table Student add(dob date default '1-Jan-99');
The above command will add a new column with default value to the Student table

To Modify an existing Column


alter command is used to modify data type of an existing column . Following is the Syntax,
alter table table-name modify(column-name datatype);
Here is an Example for this,
alter table Student modify(address varchar(30));
The above command will modify address column of the Student table

To Rename a column
Using alter command you can rename an existing column. Following is the Syntax,
alter table table-name rename old-column-name to column-name;
Here is an Example for this,
alter table Student rename address to Location;
The above command will rename address column to Location.

To Drop a Column
alter command is also used to drop columns also. Following is the Syntax,
alter table table-name drop(column-name);
Here is an Example for this,
alter table Student drop(address);

Database Keys
Keys are very important part of Relational database. They are used to establish and identify relation between
tables. They also ensure that each record within a table can be uniquely identified by combination of one or more
fields within a table.

Super Key
Super Key is defined as a set of attributes within a table that uniquely identifies each record within a table. Super
Key is a superset of Candidate key.

Candidate Key
Candidate keys are defined as the set of fields from which primary key can be selected. It is an attribute or set of
attribute that can act as a primary key for a table to uniquely identify each record in that table.

Primary Key
Primary key is a candidate key that is most appropriate to become main key of the table. It is a key that uniquely
identify each record in a table.

Composite Key
Key that consist of two or more attributes that uniquely identify an entity occurance is called Composite key. But
any attribute that makes up the Composite key is not a simple key in its own.

Secondary or Alternative key


The candidate key which are not selected for primary key are known as secondary keys or alternative keys

Non-key Attribute
Non-key attributes are attributes other than candidate key attributes in a table.

Non-prime Attribute
Non-prime Attributes are attributes other than Primary attribute.

AND & OR operator


AND and OR operators are used with Where clause to make more precise conditions for fetching data from
database by combining more than one condition together.

AND operator
AND operator is used to set multiple conditions with Where clause.

SELECT * from Emp WHERE salary < 10000 AND age > 25

OR operator
OR operator is also used to combine multiple conditions with Where clause. The only difference between AND and
OR is their behaviour. When we use AND to combine two or more than two conditions, records satisfying all the
condition will be in the result. But in case of OR, atleast one condition from the conditions specified must be
satisfied by any record to be in the result.
SELECT * from Emp WHERE salary > 10000 OR age > 25

SQL Functions

SQL provides many built-in functions to perform operations on data. These functions are useful while performing
mathematical calculations, string concatenations, sub-strings etc. SQL functions are divided into two catagories,

Aggregrate Functions

Scalar Functions

Aggregrate Functions
These functions return a single value after calculating from a group of values.Following are some frequently used
Aggregrate functions.

1) AVG()
Average returns average value after calculating from values in a numeric column.
Its general Syntax is,
SELECT AVG(column_name) from table_name

2) COUNT()
Count returns the number of rows present in the table either based on some condition or without condition.
Its general Syntax is,
SELECT COUNT(column_name) from table-name

3) FIRST()
First function returns first value of a selected column
Syntax for FIRST function is,
SELECT FIRST(column_name) from table-name

4) LAST()
LAST return the return last value from selected column
Syntax of LAST function is,
SELECT LAST(column_name) from table-name

5) MAX()
MAX function returns maximum value from selected column of the table.
Syntax of MAX function is,
SELECT MAX(column_name) from table-name

6) MIN()
MIN function returns minimum value from a selected column of the table.
Syntax for MIN function is,
SELECT MIN(column_name) from table-name

7) SUM()
SUM function returns total sum of a selected columns numeric values.
Syntax for SUM is,
SELECT SUM(column_name) from table-name

Scalar Functions
Scalar functions return a single value from an input value. Following are soe frequently used Scalar Functions.

1) UCASE()
UCASE function is used to convert value of string column to Uppercase character.
Syntax of UCASE,
SELECT UCASE(column_name) from table-name

2) LCASE()
LCASE function is used to convert value of string column to Lowecase character.
Syntax for LCASE is,
SELECT LCASE(column_name) from table-name

3) MID()
MID function is used to extract substrings from column values of string type in a table.

Syntax for MID function is,


SELECT MID(column_name, start, length) from table-name

4) ROUND()
ROUND function is used to round a numeric field to number of nearest integer. It is used on Decimal point values.
Syntax of Round function is,
SELECT ROUND(column_name, decimals) from table-name

You might also like