Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Unit-4_Introduction to Structured Query Language

Uploaded by

yugrajpurohit01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Unit-4_Introduction to Structured Query Language

Uploaded by

yugrajpurohit01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 96

Unit-4

Introduction to Structured
Query Language

Subject: Database Management System


Class: 3rd Diploma Computer
Prepared by: Divya Patel
SQL Concepts
• SQL is a standard language for storing,
manipulating and retrieving data in databases.
• Database language can be used to read, store and
update the data in database.
Database Languages
• DDL- Data Definition • DQL- Data Query
Language Language
– Create – Select
– Alter • DCL- Data Control
– Truncate
Language
– Drop
– Grant
– Rename
– Revoke
• DML- Data Manipulation • TCL- Transaction Control
Language Language
– Insert
– Commit
– Update
– Rollback
– Delete
– Savepoint
DDL- Data Definition Language
• It is used to define database structure or pattern.
• It is used to create schema, tables, indexes etc in database.
• Using the DDL statements, you can create the skeleton of the
database.
• Command:
– Create: used to create objects in DB.
– Alter: Used to alter the structure of DB.
– TRUNCATE: is used to remove all records from a table,
including all spaces allocated for the records are removed.
– Drop: used to delete objects from DB.
– Rename: Used to rename the object.
DML- Data Manipulation Language
• It is used for accessing and manipulating data in DB.
• It handles user request.
• Command:
– Insert: Used to insert data into table.
– Update: used to update existing data within table.
– Delete: used to delete records from table.
DQL- Data Query Language
• The purpose of DQL Command is to get some schema
relation based on the query passed to it.
• Command:
– Select: is used to retrieve data from a database.
DCL- Data Control Language
• DCL includes commands mainly deals with the rights,
permissions and other controls of the database system.
• Command:
– Grant-gives user’s access privileges to database.
– Revoke-withdraw user’s access privileges given by
using the Grant command.
TCL- Transaction Control Language
• TCL commands deals with the transaction within the
database.
• Commands:
– Commit– commits a Transaction.
– Rollback– rollbacks a transaction in case of any error
occurs.
– Savepoint–sets a savepoint within a transaction.
Data types
char (size):
• This datatype is used to store character string
value of fixed length.
• The size in brackets determines the number of
characters the cell can hold.
• The maximum number of characters this data type
can hold is 255 characters.
• For Example, Name char(20)
Data types (Cont..)
Varchar (size)/ varchar2 (size):
• This data type is used to store variable length
alpha numeric data.
• It is more flexible.
• The maximum this data type can hold up to 4000
characters.
Data types (Cont..)
Number (P,S):
• The number data type is used to store number
either fixed or floating point.
• Number can be stored up to 38 digits of precision.
• The precision (P) determines the maximum length
of the data, whereas the scale (S) determines the
number of places of right of decimal.
• If scale is not given then default is zero.
• For example, Contact_no number(10)
Data types (Cont..)
Date:
• This data type is used to represent date and time.
• The standard format is “DD-MON-YY” as in ’20-
AUG-20’.
• Date time stores date in the 24-hour format.
• By default, the time in a date field is 12:00:00 am,
if no time portion specified.
DDL commands
• CREATE
• ALTER
• TRUNCATE
• DROP
• RENAME
DDL Command: Create
• Create command defines each column of table uniquely.
• Each column has a minimum of three attributes: name, datatype,
size.
• Each column definition is separated from other by a comma.
• The SQL statement is terminated with semi colon.
• RULES FOR CREATING TABLES:
– A name can have may up to 30 characters
– Alphabets from A-Z, a-z and numbers 0-9 are allowed.
– A name should begin with alphabet
– The use of special character like _ is allowed.
– Reserved words of SQL not allowed.
• Syntax:
– CREATE TABLE tablename (columnname1 datatype(size),
column_name2 datatype(size), ...);
Create table with Primary key
• Syntax:
CREATE TABLE tablename
(columnname1 datatype(size) PRIMARY KEY,
column_name2 datatype(size), ...);
• For Example,
Student ( Student_ID, FName, LName, Address,
Mobile_No)

create table Student (Student_ID number(5) primary


key, FName varchar2(10), LName varchar2(10),
Address varchar2(20), Mobile_No number(10));
CREATE command with foreign key
• Syntax:
CREATE TABLE tablename
(columnname1 REFERENCES table2name (columnname),
column_name2 datatype(size), ...);
• For Example,
Student ( Student_ID, FName, LName, Address, Mobile_No)
Subject(SubjectID, SubjectName, Credits)
Studies(Student_ID, Subject_ID)

CREATE table Studies(Student_ID REFERENCES Student


(Student_ID), Subject_ID REFERENCES
Subject(SubjectID));
DDL command: ALTER
• 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.
• To ADD column in table:
Syntax:
ALTER TABLE table_name ADD ColumnName
datatype(size);

Example:
Add Hobbies column in student table.
Alter table Student ADD Hobbies varchar2(15);
DDL command: ALTER(Cont..)
• To change datatype of column in table:
Syntax:
ALTER TABLE tablename MODIFY ColumnName datatype;
Example:
change size of Hobbies to 20.
Alter table Student MODIFY Hobbies varchar2(20);

• To Drop/Delete column from table:


Syntax:
ALTER TABLE tablename DROP COLUMN ColumnName;
Example:
delete the Hobbies column.
Alter table Student DROP COLUMN Hobbies;
DDL command: TRUNCATE
• The TRUNCATE TABLE statement is used to delete the data
inside a table, but not the table itself.
• Syntax:
TRUNCATE TABLE tablename;
• Example:
TRUNCATE table Student;
DDL command: DROP
• The DROP TABLE statement is used to drop an existing
table in a database.
• Syntax:
DROP TABLE tablename;
• Example:
DROP table Student;
DDL command: RENAME
• The rename command is used to change the name of an
existing database object(like Table) to a new name.
• Syntax:
RENAME TABLE current_table_name TO new_table_name;
• Example:
RENAME table Subject to Subjects;
DML commands
• INSERT
• UPDATE
• DELETE
DML command: INSERT
• It is used for inserting data into tables.
• You can insert records/data into two manners.
1) Insert data into all columns:
Syntax:
INSERT INTO tablename VALUES (value1, value2, …);
Example:
insert into Student (Student_ID, Fname, Lname, Address,
Mobile_no) values (1001, ‘Ali’, ‘Shaikh’, ‘Surat’, 987654321);
DML command: INSERT
2) Insert data into specific columns:
Syntax:
INSERT INTO tablename (columnname1,
columnname2, …) VALUES (value1, value2, …);
Example:
insert into Student (Student_ID, Fname, Lname,
Address, Mobile_no) values (1001, ‘Ali’, ‘Shaikh’, ‘Surat’,
987654321);
DML command: UPDATE
• The UPDATE statement is used to modify the existing records
in a table.
• Update/insert data into whole column:
Syntax:
UPDATE table_name SET column1=value1;
Example:
UPDATE Student SET Hobbies=‘Reading’;
• Update data into specific row:
Syntax:
UPDATE table_name SET column1=value1,.. WHERE condition;
Example:
UPDATE Student SET Hobbies=‘Traveling’ where
Fname=‘Ankit’;
DML command: DELETE
• The DELETE statement is used to delete existing records in
a table.
• Delete all records from table:
Syntax:
DELETE FROM table_name;
Example:
DELETE from Student ;

• Delete specific records from table:


Syntax:
DELETE FROM table_name WHERE condition;
Example:
DELETE from Student where Fname=‘Ankit’;
DQL command: SELECT
• It is used to fetch data from a database.
• To display all data from table:
Syntax:
SELECT * FROM tablename;
Example:
Select * from Student;
• To display specific column from table:
Syntax:
SELECT columnname1, columnname2,… FROM
tablename;
Example:
Select Fname, Lname from Student;
SELECT command with WHERE clause
• It is used to filter records.
• It is used to extract only those records that fulfill a
specified condition.
• Mostly condition is in form of:
Columnname Operator Value
• Syntax:
SELECT columnname1, columnname2,… FROM
tablename WHERE condition;
• Example:
Select Fname, Lname from Student where
Student_Id=1004;
Arithmetic Operators
Operators Description
+ Add
- Subtract
* Multiply
/ Divide
% Modulo
Comparison Operators
Operators Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
!= Not equal to
Logical operators
Operators Description
AND TRUE if all the conditions separated by AND
is TRUE
OR TRUE if any of the conditions separated by
OR is TRUE
NOT Displays a record if the condition(s) is NOT
TRUE
BETWEEN TRUE if the operand is within the range of
comparison
LIKE TRUE if the operand matches a pattern
IN TRUE if the operand is equal to one of a list
of expression
DESC command
• DESC command use for describe the list of column
definitions for specified table.
• DESC statement to get following information:
– Column Name
– Column allow NULL or NOT NULL
– Datatype of the Column
– With database size precision and If NUMERIC datatype scale.
• Syntax:
DESC tablename;
• Example:
DESC Student;
Distinct in SQL
• The SELECT DISTINCT statement is used to return only
distinct (different) values.
• Syntax:
SELECT DISTINCT(columnname) FROM tablename;
• Example:
Select DISTINCT(Fname) from Student;
SQL Functions
• Single row functions
– Numeric functions
– Character functions
– Conversion functions
– Date functions
– Miscellaneous functions
• Group functions
Single row functions
• Single row functions can be character functions,
numeric functions, date functions, and conversion
functions.
• Note that these functions are used to manipulate
data items.
• These functions require one or more input
arguments and operate on each row, thereby
returning one output value for each row.
• Argument can be a column, literal or an expression.
Single row functions can be used in SELECT
statement, WHERE and ORDER BY clause.
Numeric functions
• ABS() • MOD()
• CEIL() • POWER()
• COS() • ROUND()
• EXP() • SQRT()
• FLOOR() • SIN()
Numeric function: ABS()
• ABS():
It will convert given value in positive number.
Syntax:
ABS(number)
Example:
select ABS(-18) from dual;
Output:
18
Numeric function: CEIL()
• CEIL():
It will returns the smallest integer value that is
bigger than or equal to a number.
Syntax:
CEIL(number)
Example:
select CEIL(17.08) from dual;
Output:
18
Numeric function: FLOOR()
• FLOOR()
It returns the largest integer value that is smaller
than or equal to a number.
Syntax:
FLOOR(number)
Example:
select FLOOR(17.08) from dual;
Output:
17
Numeric function: EXP()
• EXP()
It will returns e raised to the power of the specified
number.
Syntax:
EXP(number)
Example:
select EXP(1) from dual;
Output:
2.71828182845904523536028747135266249776
Numeric function: COS()
• COS()
It will return the cosine of a number.
Syntax:
COS(number)
Example:
select COS(0) from dual;
Output:
1
Numeric function: SIN()
• SIN()
It will return the sine of a number.
Syntax:
SIN(number)
Example:
select SIN(0) from dual;
Output:
0
Numeric function: MOD()
• MOD()
It returns the remainder of a number divided by
another number.
Syntax:
MOD(x, y)
where, x is a value that will be divided by y.
Example:
select MOD(18,4) from dual;
Output:
2
Numeric function: POWER()
• POWER()
It returns the value of a number raised to the
power of another number.
Syntax:
POWER(x, y)
Example:
select POWER(8,3) from dual;
Output:
512
Numeric function: ROUND()
• ROUND()
It rounds a number to a specified number of
decimal places.
Syntax:
ROUND(number, decimals)
Example:
select ROUND(135.375, 2) from dual;
Output:
135.38
Numeric function: SQRT()
• SQRT()
It returns the square root of a number.
Syntax:
SQRT(number)
Example:
select SQRT(13) from dual;
Output:
3.60555127546398929311922126747049594625
Character functions
• ASCII() • INITCAP()
• CONCAT() • LPAD()
• REVERSE() • RPAD()
• LENGTH() • TRIM()
• LOWER() • TRANSLATE()
• UPPER() • SUBSTR()
Character function: ASCII()
• ASCII()
It returns the ASCII value for the specific character.
Syntax:
ASCII(character)
Example:
select ASCII(‘D’) from dual;
Output:
68
Character function: CONCAT()
• CONCAT()
It adds two or more strings together.
Syntax:
CONCAT(string1, string2)
Example:
select CONCAT(‘Database’, ‘ Management’)
from dual;
Output:
Database Management
Character function: REVERSE()
• REVERSE()
It reverses a string and returns the result.
Syntax:
REVERSE(string)
Example:
select REVERSE(‘Database’) from dual;
Output:
esabataD
Character function: LENGTH()
• LENGTH()
It returns the length of a string.
Syntax:
LENGTH(string1)
Example:
select LENGTH(‘Database Management
System’) from dual;
Output:
26
Character function: LOWER()
• LOWER()
It converts a string to lower-case.
Syntax:
LOWER(string)
Example:
select LOWER(‘CHARACTER Functions in SQL’)
from dual;
Output:
character functions in sql
Character function: UPPER()
• UPPER()
It converts a string to upper-case.
Syntax:
UPPER(string)
Example:
select UPPER(‘Character Functions in SQL’)
from dual;
Output:
CHARACTER FUNCTIONS IN SQL
Character function: INITCAP()
• INITCAP()
It will make all the first letters of given words into
capital letter.
Syntax:
INITCAP(string)
Example:
select INITCAP(‘CHARACTER functions in
SQL’) from dual;
Output:
Character Functions In Sql
Character function: LPAD()
• LPAD()
The LPAD() function left-pads a string with another string, to a
certain length.
Syntax:
LPAD(string, length, lpad_string)
Parameters:
String: The original string.
length: The length of the string after it has been left-
padded.
lpad_string: The string to left-pad to string.
Example:
SELECT LPAD(‘Diploma’, 20, ‘*@*’) from dual;
Output:
*@**@**@**@**Diploma
Character function: RPAD()
• RPAD()
The LPAD() function right-pads a string with another string, to a
certain length.
Syntax:
RPAD(string, length, rpad_string)
Parameters:
String: The original string.
length: The length of the string after it has been right-
padded.
lpad_string: The string to right-pad to string.
Example:
SELECT RPAD(‘Diploma’, 20, ‘*@*’) from dual;
Output:
Diploma*@**@**@**@**
Character function: TRIM()
• TRIM()
It is basically used to remove extra spaces from a given string. But by
using trim function we can also remove any character from starting or
ending of a string.
Syntax:
TRIM(LEADING/TRAILING/BOTH ‘character_to_trim’ FROM
‘string’)
where, leading: to trim character from starting
trailing: to trim character from ending
both: to trim character from both side
Example:
select ltrim(‘ hello ') from dual – It will remove left side’s extra spaces.
select TRIM(leading '*' from '***hello***') from dual; Output: hello***
select TRIM(trailing '*' from '***hello***') from dual; Output: ***hello
select TRIM(BOTH '*' from '***hello***') from dual; Output: hello
Character function: TRANSLATE()
• TRANSLATE()
It will take three arguments as a parameter. And replace
2nd arguments values with 3rd arguments values in a
string given as 1st argument.
Syntax:
TRANSLATE(string,‘characters_to_replace’,
‘replacement_characters’)
Example:
select TRANSLATE(‘Computer Engineering’, ‘ei’, ‘@!’)
from dual;
Output:
Comput@r Eng!n@@r!ng
Character function: SUBSTR()
• SUBSTR()
The SUBSTR() function extracts a substring from a string
(starting at any position).
Syntax:
SUBSTR(string, start, length)
Parameters:
String: The string to extract from
start: The start position.
length: Optional. The number of characters to extract. If
omitted, the whole string will be returned(from
the start position)
Example:
Select SUBSTR('Database Management System', 10, 6) from
dual;
Output:
Manage
Conversion functions
• TO_CHAR(number)
• TO_CHAR(date)
• TO_NUMBER()
• TO_DATE()
Conversion function: TO_CHAR()
• TO_CHAR() function converts a DATE or number in a specified
string format.
• Syntax:
TO_CHAR(expr, string_format);
• Arguments:
1) expr
The expr is a DATE or an INTERVAL value that should be
converted.

2) string_format
The string_format is a string that determines the format that the
result string should be in.
The string_format argument is optional.
Example of to_char() with number
• Select TO_CHAR(1210.73, '9999.9') from dual;
Result: ' 1210.7'

• Select TO_CHAR(1210.73, '9,999.99') from dual;


Result: ' 1,210.73'

• Select TO_CHAR(1210.73, '$9,999.99') from dual;


Result: ' $1,210.73'

• Select TO_CHAR(21, '000099') from dual;


Result: ' 000021‘
Example of to_char() with Date
• Select TO_CHAR(sysdate, 'yyyy/mm/dd') from dual;
Result: '2020/09/05'

• Select TO_CHAR(sysdate, 'Month DD, YYYY') from dual;


Result: ‘September 05, 2020’

• Select TO_CHAR(sysdate, 'FMMonth DD, YYYY') from dual;


Result: ' September 05, 2020’

• Select TO_CHAR(sysdate, 'MON DDth, YYYY') from dual;


Result: ‘SEP 05TH, 2020’

• Select TO_CHAR(sysdate, 'FMMON DDth, YYYY') from dual;


Result: ‘SEP 5TH, 2020’

• Select TO_CHAR(sysdate, 'FMMon ddth, YYYY') from dual;


Result: ‘Sep 5th, 2020’
Conversion function: TO_NUMBER()
• TO_NUMBER() function converts a string to number.
• Syntax:
TO_NUMBER(string, format_mask)
• Arguments:
1) string
The string that will be converted to a number.

2) format_mask
This is the format that will be used to
convert string1 to a number.
The format_mask is optional.
Example of to_number()
• Select TO_NUMBER('1210.73', '9999.99') from dual;
Result: 1210.73
• Select TO_NUMBER('546', '999') from dual;
Result: 546
• Select TO_NUMBER('23', '99') from dual;
Result: 23
Conversion function: To_date()
• TO_DATE function converts a string to a date.
• Syntax:
TO_DATE( string1 [, format_mask])
• Arguments:
String1
The string that will be converted to a date.
format_mask
Optional.
This is the format that will be used to convert string1 to a
date.
Example of To_date()
• SELECT TO_DATE('2020/09/05', 'yyyy/mm/dd') from
dual;
Result: 09/05/2020

• SELECT TO_DATE('090712', ‘YYDDMM') from dual;


Result: 12/07/2009

• SELECT TO_DATE('09072020', 'mmddyyyy') from dual;


Result: 09/07/2020
Date functions
• LAST_DAY()
• MONTHS_BETWEEN()
• ADD_MONTHS()
• CURRENT_DATE
• NEXT_DAY()
• SYSDATE
• SYSTIMESTAMP
Date function: LAST_DAY()
• The LAST_DAY() function extracts the last day of the month
for a given date.
• Syntax:
LAST_DAY(date)
• Argument:
date: It is required. The date to extract the last day of the
month from.
• Example,
SELECT LAST_DAY(‘02/12/2020’) from dual;
Output: 02/29/2020
SELECT LAST_DAY(SYSDATE) from dual;
Output: 09/30/2020
Date function: Months_between()
• The MONTHS_BETWEEN() function is used to get the
number of months between dates (date1, date2).
• Syntax:
MONTHS_BETWEEN(date1,date2)
• See the following conditions:
– If date1 is later than date2, then the result is positive.
– If date1 is earlier than date2, then the result is negative.
– If date1 and date2 are either the same days of the month or both
last days of months, then the result is always an integer.
– Otherwise, Oracle Database calculates the fractional portion of the
result based on a 31-day month and considers the difference in
time components date1 and date2.
Example of months_between()
• select months_between('09/07/2020','08/07/2020') from
dual;
Result: 1

• select months_between('09/07/2020',‘10/07/2020') from


dual;
Result: -1

• select months_between(’09/30/2020',’08/31/2020') from


dual;
Result: 1
Date function: ADD_MONTHS()
• ADD_MONTHS() function adds a number of month(n) to
a date and returns the same day n of month away.
• Syntax:
ADD_MONTHS(date_expression, month)
• Argument:
date_expression: The date_expression argument is a DATE
value or any expression that evaluates to a DATE value to
which the number of month is added.
month: The month argument is an integer that represents a
number of months which adds to the first argument.
The month argument can be zero, positive or negative. A
positive month value allows you to go forward in month while
a negative month value bring you backward in month.
Example of add_months()
• select add_months('09/07/2020',2) from dual;
Result: 11/07/2020

• select add_months('09/07/2020‘, 0) from dual;


Result: 09/07/2020

• select add_months(’09/30/2020‘, -4) from dual;


Result: 05/31/2020
Date function: CURRENT_DATE
• CURRENT_DATE function returns the current date in the
session time zone.
• Syntax:
CURRENT_DATE
• Example:
select CURRENT_DATE from dual;
Output:
09/26/2020
Date function: NEXT_DAY()
• NEXT_DAY() function returns the date of the first weekday
specified by day name that is later than a date.
• Syntax:
NEXT_DAY(date,weekday)
• Argument:
date: It is a DATE value or an expression that evaluates to
a DATE value which is used to find the next weekday.
weekday: It is the day of week that you want to return.
The weekday can be full name e.g., Tuesday or abbreviation
e.g., Tue.
Example of Next_day()
• select next_day('09/16/2020', 'Wednesday‘) FROM dual;
Output: 09/23/2020

• select next_day('09/16/2020', ‘Saturday‘) FROM dual;


Output: 09/19/2020

• select next_day('09/16/2020', ‘Thu‘) FROM dual;


Output: 09/17/2020
Date function: SYSDATE
• SYSDATE function returns the current date and time of the
Operating System (OS) where the Oracle Database
installed.
• Syntax:
SYSDATE
• Example:
select SYSDATE from dual;
Output:
09/26/2020
Date function: SYSTIMESTAMP
• SYSTIMESTAMP function returns a TIMESTAMP WITH
TIME ZONE value that represents the system date and time
including fractional seconds and time zone.
• Syntax:
SYSTIMESTAMP
• Example:
select SYSTIMESTAMP from dual;
Output:
26-SEP-20 04.47.09.751232 AM +00:00
Group functions
• Group functions are built-in SQL functions that
operate on groups of rows and return one value for the
entire group.
• The group functions are as follow:
– AVG()
– SUM()
– MIN()
– MAX()
– COUNT()
– COUNT(*)
– DISTINCT()
Group function: AVG()
• AVG()
It is used to find average of a given column’s
values.
Syntax:
select AVG(column_name) from tablename;
Example:
select AVG(Credits) from Subject;
Output:
24.2
Group function: SUM()
• SUM()
It is used to perform summation operation on
given column’s values.
Syntax:
select SUM(column_name) from tablename;
Example:
select SUM(Credits) from Subject;
Output:
121
Group function: MIN()
• MIN()
It is used to find minimum number from given
column’s values.
Syntax:
select MIN(column_name) from tablename;
Example:
select MIN(Credits) from Subject;
Output:
23
Group function: MAX()
• MAX()
It is used to find maximum number from given
column’s values.
Syntax:
select MAX(column_name) from tablename;
Example:
select MAX(Credits) from Subject;
Output:
25
Group function: COUNT()
• COUNT()
It is used to count total number of rows in given
column.
Syntax:
select COUNT(column_name) from tablename;
Example:
select COUNT(Credits) from Subject;
Output:
5
Group function: COUNT(*)
• COUNT(*)
It will count total number of rows in a given table.
Syntax:
select COUNT(*) from tablename;
Example:
select COUNT(*) from Subject;
Output:
5
Group function: DISTINCT()
• DISTINCT()
It is used to display unique values from given
column.
Syntax:
select DISTINCT(column_name) from
tablename;
Example:
Display unique name of all students.
select DISTINCT(Fname) from Student;
How many unique names are in Student table?
select COUNT(DISTINCT(Fname)) from
Student;
Miscellaneous functions
• UID
• USER
• DECODE()
• NVL()
• VSIZE()
• GREATEST()
• LEAST()
Miscellaneous functions: UID, USER
• UID
Returns an integer value corresponding to UserId
of the user currently logged in.
select UID from dual;
Output:
73373
• USER
Returns the user name of currently logged in.
select USER from dual;
Output:
APEX_PUBLIC_USER
Miscellaneous functions: DECODE()
• DECODE()
It is similar to if-then-else construct in
programming language.
Syntax:
DECODE(value, if1, then1, if2, then2,… ,else)
For example,
select DECODE(‘Saturday’, ‘Sunday’, ‘Holiday’,
‘Saturday’, ‘Half day’, ‘Full day’) from dual;
Output:
Half day
Miscellaneous functions: NVL()
• NVL()
Syntax:
NVL(exp1, exp2);
Returns exp2 if exp1 is null and Returns exp1 if it
is not null. Applicable to numeric, character or
date data type.
Example,
select NVL(10, 15) from dual;
Output: 10
select NVL(null, 20) from dual;
Output: 20
Miscellaneous functions: VSIZE()
• VSIZE()
Returns storage size of expression.
Syntax:
VSIZE(exp);
Example:
select VSIZE(‘Database Management system’)
from dual;
Output: 26
select VSIZE(1456723) from dual;
Output: 5
Miscellaneous functions: GREATEST()
• GREATEST()
It is used to find greater values from given values.
Syntax:
GREATEST(value 1, value 2, …, value n);
Example,
select greatest(34, 675, 21, 8789, 6745) from dual;
Output: 8789
select greatest(‘D’, ‘i’, ‘p’ ,’l’, ‘o’, ‘m’, ‘a’) from dual;
Output: p
select greatest('?','@','#','$','.') from dual;
Output: @
Miscellaneous functions: LEAST()
• LEAST()
It is used to find smaller values from given values.
Syntax:
LEAST(value 1, value 2, …, value n);
Example,
select least(34, 675, 21, 8789, 6745) from dual;
Output: 21
select least(‘D’, ‘i’, ‘p’ ,’l’, ‘o’, ‘m’, ‘a’) from dual;
Output: D
select least('?','@','#','$','.') from dual;
Output: #
Group by clause
• The GROUP BY statement groups 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.
• Syntax:
SELECT column_name(s) FROM table_name WHERE
condition GROUP BY column_name(s);
Example:
List the total number of students who belongs to same city.
select address, count(Student_ID) from student group
by address;
Having clause
• The HAVING clause was added to SQL because the WHERE
keyword could not be used with aggregate functions.
• Syntax:
SELECT column_name(s) FROM table_name WHERE
condition GROUP BY column_name(s) HAVING condition;
• Example:
List out total number of students who are studying more
than 1 subject.
select count(student_id),subject_id from studies group by
subject_id having count(student_id)>1;
Order by clause
• 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.
• To arrange in ascending order (Syntax):
SELECT column1, column2, ... FROM table_name
ORDER BY column1;
• To arrange in descending order (Syntax):
SELECT column1, column2, ... FROM table_name
ORDER BY column1 DESC;

You might also like