SlideShare a Scribd company logo
Database Query Using SQL
Lets do practical on DATABASE…
SORTING OUTPUT
By default records will come in the output in the same order in which it was
entered. Tosee the output rows in sorted or arranged in ascending or descending
order SQL provide ORDER BY clause. By default output will be ascending
order(ASC) to see output in descending order we use DESC clause with ORDER BY
.
Select * from emp order by name; (ascending order)
Select * from emp order by salary desc;
Select * from emp order by dept asc, salary desc;
AGGREGATEfunctions
Aggregate function is used to perform calculation on group of rows and return the
calculated summary like sum of salary,averageof salary etc.
Available aggregatefunctions are –
1. SUM()
2. AVG()
3. COUNT()
4. MAX()
5. MIN()
6. COUNT(*)
AGGREGATEfunctions
Select SUM(salary) from emp;
Output – 161000
Select SUM(salary) from emp where dept=‘sales’;
Output - 59000
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
AGGREGATEfunctions
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
Select AVG(salary) from emp;
Output – 32200
Select AVG(salary) from emp where dept=‘sales’;
Output - 29500
AGGREGATEfunctions
Select COUNT(name) from emp;
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
Output – 5
Select COUNT(salary) from emp where dept=‘HR’;
Output - 1
Select COUNT(DISTINCT dept)from emp;
Output - 3
AGGREGATEfunctions
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
Select MAX(Salary) from emp;
Output – 45000
Select MAX(salary) from emp where dept=‘Sales’;
Output - 35000
AGGREGATEfunctions
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
Select MIN(Salary) from emp;
Output – 24000
Select MIN(salary) from emp where dept=‘IT’;
Output - 27000
AGGREGATEfunctions
Empno Name Dept Salary
1 Ravi Sales 24000
2 Sunny Sales 35000
3 Shobit IT 30000
4 Vikram IT 27000
5 nitin HR 45000
6 Krish HR
Select COUNT(*)from emp;
Output – 6
Select COUNT(salary) from emp;
Output - 5
count(*) Vs count()
Count(*) function is used to count the number of rows in query
output whereas count() is used to count values present in any
column excludingNULL values.
Note:
All aggregatefunction ignores the NULL values.
GROUP BY
GROUP BY clause is used to divide the table into logical groups and we can
perform aggregate functions in those groups. In this case aggregate function
will return output for each group. For example if we want sum of salary of each
department we have to divide table records.
Aggregate functions by default takes the entire table as a single group that’s why we are
getting the sum(), avg(), etc output for the entire table. Now suppose organizationwants the
sum() of all the job separately,or wants to find the average salary of every job. In this case
we have to logically divide our table into groups based on job, so that every group will be
passed to aggregate function for calculation and aggregate function will return the result for
every group.
Group by clause helps up to divide the table into logical groups based on any
column value. In those logically divided records we can apply aggregate
functions.For.E.g.
SELECT SUM(SAL) FROM EMP GROUP BY DEPT;
SELECT JOB,SUM(SAL) FROM EMP GROUP BY
DEPT;
SELECT JOB,SUM(SAL),AVG(SAL),MAX(SAL),COUNT(*) EMPLOYEE_COUNTFROM EMP;
NOTE :- when we are using GROUP BY we can use only aggregate function and the column
on which we are grouping in the SELECT list because they will form a group other than any
column will gives you an error because they will be not the part of the group.
For e.g.
SELECT ENAME,JOB,SUM(SAL) FROM EMP GROUP BY JOB;
Error -> because Ename is not a group expression
HAVING with GROUP BY
• If we wantto filter or restrict some rowsfrom the output produced by GROUP BYthen we use HAVING
clause. It is used to put condition of group of rows. With having clause we can use aggregatefunctions
also.
• WHERE is used before the GROUP BY.With WHEREwe cannot use aggregatefunction.
• E.g.
• SELECT DEPT,AVG(SAL)FROM EMP GROUP BYDEPT HAVINGJOB IN (‘HR’,’SALES’
)
• SELECT DEPT,MAX(SAL),MIN(SAL),COUNT(*)FROM EMP GROUP BYDEPT HAVING
COUNT(*)>2
• SELECT DEPT,MAX(SAL),MIN(SAL)FROM EMP WHERE SAL>=2000 GROUP BYDEPT HAVING
DEPT IN(‘IT’,’HR’)
MYSQL FUNCTIONS
A function is built – in code for specific purpose that takesvalue and returns a
single value. Valuespassed to functions are known as arguments/parameters.
There are various categories of function in MySQL:-
1) String Function
2) Mathematical function
3) Date and time function
Function Description Example
CHAR() Return character for
given ASCII Code
Select Char(65);
Output- A
CONCAT() Return concatenated
string
Select concat(name, ‘ works in ‘, dept,’ department ’);
LOWER()/
LCASE()
Return string in small
letters
Select lower(‘INDIA’); Output- india
Select lower(name) from emp;
SUBSTRING(S,
P,N) /
MID(S,P,N)
Return N character of
string S, beginning from
P
Select SUBSTRING(‘LAPTOP’,3,3); Output – PTO
Select SUBSTR(‘COMPUTER’,4,3); Output – PUT
UPPER()/
UCASE()
Return string in capital
letters
Select Upper(‘india’); Output- INDIA
LTRIM() Removes leading space Select LTRIM(‘ Apple’); Output- ‘Apple’
RTRIM Remove trailing space Select RTRIM(‘Apple ‘); Output- ‘Apple’
String Function
Function Description Example
TRIM() Remove spaces from
beginning and ending
Select TRIM(‘ Apple ‘); Output-’Apple’
Select* fromemp where trim(name) = ‘Suyash’;
INSTR() It search one string in
another string and
returns position, if not
found 0
SelectINSTR(‘COMPUTER’,’PUT’); Output-4
Select INSTR(‘PYTHON’,’C++’); Output – 0
LENGTH() Returns number of
character in string
Selectlength(‘python’); Output- 7 Select
name, length(name) fromemp
LEFT(S,N) Return N characters of S
from beginning
SelectLEFT(‘KV NO1 TEZPUR’,2); Output-KV
String Function
RIGHT(S,N) Return N characters of S
from ending
Select RIGHT(‘KV NO1 ’,3); Output- NO1
Function Description Example
MOD(M,N) Return remainderM/N SelectMOD(11,5); Output- 1
POWER(B,P) Return B to power P SelectPOWER(2,5); Output-32
ROUND(N,D) Return number rounded to D
place after decimal
SelectROUND(11.589,2); Output- 11.59
SelectROUND(12.999,2); Output- 13.00
SelectROUND(267.478,-2) OUTPUT- 300
SIGN(N) Return -1 for –ve number 1 for
ve
+ number
Selectsign(-10) Output : -1
Selectsign(10); Output : 1
SQRT(N) Returns square rootof N SelectSQRT(144);Output: 12
TRUNCATE(M,
N)
Return number upto N place after
decimal without rounding it
SelectTruncate(15.789,2);Output: 15.79
Numeric Function
Function Description Example
CURDATE()/
CURRENT_DATE()/
CURRENT_DATE
Return the current date Selectcurdate(); Select current_date();
DATE() Return date part from
datetime expression
Select date(‘2018-08-15 12:30’); Output:
2018-08-15
MONTH() Return month fromdate Selectmonth(‘2018-08-15’);Output: 08
YEAR() Return year fromdate Selectyear(‘2018-08-15’);Output: 2018
DAYNAME() Return weekday name Select dayname(‘2018-12-04’); Output:
Tuesday
DAYOFMONTH() Return value from1-31 Select dayofmonth(‘2018-08-15’) Output:
15
DAYOFWEEK() Return weekday index, for
Sunday-1, Monday-2, ..
Select dayofweek(‘2018-12-04’); Output:
3
Date and Time Function
DAYOFYEAR() Return value from1-366 Select dayofyear(‘2018-02-10’) Output:
41
Date and Time Function
Function Description Example
NOW() Return both current date and time Select now();
at which the function executes
SYSDATE() Return both current date and time Select sysdate()
Difference Between NOW() and SYSDATE():
NOW() function return the date and time at which function was executed even if we
execute multiple NOW() function with select. whereas SYSDATE() will always return
date and time at which each SYDATE() function started execution. For example.
mysql> Select now(), sleep(2), now();
Output: 2018-12-04 10:26:20, 0, 2018-12-04 10:26:20
mysql> Select sysdate(), sleep(2), sysdate();
Output: 2018-12-04 10:27:08, 0, 2018-12-04 10:27:10
Database Query Using SQL_ip.docx
Ad

More Related Content

What's hot (20)

Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
Pankaj Thakur
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
Nikhil Pandit
 
DML Commands
DML CommandsDML Commands
DML Commands
Randy Riness @ South Puget Sound Community College
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
Zidny Nafan
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
ammarbrohi
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
Hammad Rasheed
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
Shrija Madhu
 
Python Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String MethodsPython Programming Essentials - M8 - String Methods
Python Programming Essentials - M8 - String Methods
P3 InfoTech Solutions Pvt. Ltd.
 
Dbms lab questions
Dbms lab questionsDbms lab questions
Dbms lab questions
Parthipan Parthi
 
Oracle Database Advanced Querying
Oracle Database Advanced QueryingOracle Database Advanced Querying
Oracle Database Advanced Querying
Zohar Elkayam
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ratnakar Mikkili
 
Normalization in DBMS
Normalization in DBMSNormalization in DBMS
Normalization in DBMS
Prateek Parimal
 
Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav2910
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1
asmhemu
 
Call by value
Call by valueCall by value
Call by value
Dharani G
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Spanning trees & applications
Spanning trees & applicationsSpanning trees & applications
Spanning trees & applications
Tech_MX
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
Sql
SqlSql
Sql
Aman Lalpuria
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 

Similar to Database Query Using SQL_ip.docx (20)

SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
MrHello6
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
Nitesh Singh
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
अभिषेक शर्मा
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
Dr. C.V. Suresh Babu
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
ssuserc5aa74
 
SQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commandsSQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commands
sonali sonavane
 
Module03
Module03Module03
Module03
Sridhar P
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
Sachin Shukla
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
mukesh24pandey
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
Yanli Liu
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
SabrinaShanta2
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
Dr. C.V. Suresh Babu
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
Mudasir Syed
 
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
sahilurrahemankhan
 
SQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdf
SQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdfSQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdf
SQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdf
SudhanshuPandey222889
 
Oracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |ThrissurOracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |Thrissur
IndiaOptions Softwares
 
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHONAGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
examcelldavrng
 
Les03
Les03Les03
Les03
arnold 7490
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
Vineeta Garg
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
waqaszaidi91
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
Nitesh Singh
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
ssuserc5aa74
 
SQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commandsSQL Data Manipulation language and DQL commands
SQL Data Manipulation language and DQL commands
sonali sonavane
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
mukesh24pandey
 
Oracle tips and tricks
Oracle tips and tricksOracle tips and tricks
Oracle tips and tricks
Yanli Liu
 
12. Basic SQL Queries (2).pptx
12. Basic SQL Queries  (2).pptx12. Basic SQL Queries  (2).pptx
12. Basic SQL Queries (2).pptx
SabrinaShanta2
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
Mudasir Syed
 
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
0716330552518_DBMS_LAB_THEORY_SQL_OPERATOR (1).pdf
sahilurrahemankhan
 
SQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdf
SQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdfSQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdf
SQL Top 10 Interview QnA By Rishabh Mishra in Hindi.pdf
SudhanshuPandey222889
 
Oracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |ThrissurOracle Training in Kochi | Trivandrum |Thrissur
Oracle Training in Kochi | Trivandrum |Thrissur
IndiaOptions Softwares
 
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHONAGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
AGGREGATE FUNCTION SWITH SYNTAX FOR EASY LEARNING OF PYTHON
examcelldavrng
 
Structured query language functions
Structured query language functionsStructured query language functions
Structured query language functions
Vineeta Garg
 
Ad

More from VandanaGoyal21 (9)

sql_data.pdf
sql_data.pdfsql_data.pdf
sql_data.pdf
VandanaGoyal21
 
sample project-binary file.docx
sample project-binary file.docxsample project-binary file.docx
sample project-binary file.docx
VandanaGoyal21
 
STACK.docx
STACK.docxSTACK.docx
STACK.docx
VandanaGoyal21
 
Computer Networks.docx
Computer Networks.docxComputer Networks.docx
Computer Networks.docx
VandanaGoyal21
 
Computer Networks.docx
Computer Networks.docxComputer Networks.docx
Computer Networks.docx
VandanaGoyal21
 
functions.docx
functions.docxfunctions.docx
functions.docx
VandanaGoyal21
 
Functions.docx
Functions.docxFunctions.docx
Functions.docx
VandanaGoyal21
 
CLASS 10 IT PRACTICAL FILE.pdf
CLASS 10 IT PRACTICAL FILE.pdfCLASS 10 IT PRACTICAL FILE.pdf
CLASS 10 IT PRACTICAL FILE.pdf
VandanaGoyal21
 
WEB APPLICATIONS 1.pdf
WEB APPLICATIONS 1.pdfWEB APPLICATIONS 1.pdf
WEB APPLICATIONS 1.pdf
VandanaGoyal21
 
Ad

Recently uploaded (20)

YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
National Information Standards Organization (NISO)
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Engage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdfEngage Donors Through Powerful Storytelling.pdf
Engage Donors Through Powerful Storytelling.pdf
TechSoup
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 

Database Query Using SQL_ip.docx

  • 1. Database Query Using SQL Lets do practical on DATABASE…
  • 2. SORTING OUTPUT By default records will come in the output in the same order in which it was entered. Tosee the output rows in sorted or arranged in ascending or descending order SQL provide ORDER BY clause. By default output will be ascending order(ASC) to see output in descending order we use DESC clause with ORDER BY . Select * from emp order by name; (ascending order) Select * from emp order by salary desc; Select * from emp order by dept asc, salary desc;
  • 3. AGGREGATEfunctions Aggregate function is used to perform calculation on group of rows and return the calculated summary like sum of salary,averageof salary etc. Available aggregatefunctions are – 1. SUM() 2. AVG() 3. COUNT() 4. MAX() 5. MIN() 6. COUNT(*)
  • 4. AGGREGATEfunctions Select SUM(salary) from emp; Output – 161000 Select SUM(salary) from emp where dept=‘sales’; Output - 59000 Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000
  • 5. AGGREGATEfunctions Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 Select AVG(salary) from emp; Output – 32200 Select AVG(salary) from emp where dept=‘sales’; Output - 29500
  • 6. AGGREGATEfunctions Select COUNT(name) from emp; Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 Output – 5 Select COUNT(salary) from emp where dept=‘HR’; Output - 1 Select COUNT(DISTINCT dept)from emp; Output - 3
  • 7. AGGREGATEfunctions Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 Select MAX(Salary) from emp; Output – 45000 Select MAX(salary) from emp where dept=‘Sales’; Output - 35000
  • 8. AGGREGATEfunctions Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 Select MIN(Salary) from emp; Output – 24000 Select MIN(salary) from emp where dept=‘IT’; Output - 27000
  • 9. AGGREGATEfunctions Empno Name Dept Salary 1 Ravi Sales 24000 2 Sunny Sales 35000 3 Shobit IT 30000 4 Vikram IT 27000 5 nitin HR 45000 6 Krish HR Select COUNT(*)from emp; Output – 6 Select COUNT(salary) from emp; Output - 5
  • 10. count(*) Vs count() Count(*) function is used to count the number of rows in query output whereas count() is used to count values present in any column excludingNULL values. Note: All aggregatefunction ignores the NULL values.
  • 11. GROUP BY GROUP BY clause is used to divide the table into logical groups and we can perform aggregate functions in those groups. In this case aggregate function will return output for each group. For example if we want sum of salary of each department we have to divide table records.
  • 12. Aggregate functions by default takes the entire table as a single group that’s why we are getting the sum(), avg(), etc output for the entire table. Now suppose organizationwants the sum() of all the job separately,or wants to find the average salary of every job. In this case we have to logically divide our table into groups based on job, so that every group will be passed to aggregate function for calculation and aggregate function will return the result for every group.
  • 13. Group by clause helps up to divide the table into logical groups based on any column value. In those logically divided records we can apply aggregate functions.For.E.g. SELECT SUM(SAL) FROM EMP GROUP BY DEPT; SELECT JOB,SUM(SAL) FROM EMP GROUP BY DEPT; SELECT JOB,SUM(SAL),AVG(SAL),MAX(SAL),COUNT(*) EMPLOYEE_COUNTFROM EMP; NOTE :- when we are using GROUP BY we can use only aggregate function and the column on which we are grouping in the SELECT list because they will form a group other than any column will gives you an error because they will be not the part of the group. For e.g. SELECT ENAME,JOB,SUM(SAL) FROM EMP GROUP BY JOB; Error -> because Ename is not a group expression
  • 14. HAVING with GROUP BY • If we wantto filter or restrict some rowsfrom the output produced by GROUP BYthen we use HAVING clause. It is used to put condition of group of rows. With having clause we can use aggregatefunctions also. • WHERE is used before the GROUP BY.With WHEREwe cannot use aggregatefunction. • E.g. • SELECT DEPT,AVG(SAL)FROM EMP GROUP BYDEPT HAVINGJOB IN (‘HR’,’SALES’ ) • SELECT DEPT,MAX(SAL),MIN(SAL),COUNT(*)FROM EMP GROUP BYDEPT HAVING COUNT(*)>2 • SELECT DEPT,MAX(SAL),MIN(SAL)FROM EMP WHERE SAL>=2000 GROUP BYDEPT HAVING DEPT IN(‘IT’,’HR’)
  • 15. MYSQL FUNCTIONS A function is built – in code for specific purpose that takesvalue and returns a single value. Valuespassed to functions are known as arguments/parameters. There are various categories of function in MySQL:- 1) String Function 2) Mathematical function 3) Date and time function
  • 16. Function Description Example CHAR() Return character for given ASCII Code Select Char(65); Output- A CONCAT() Return concatenated string Select concat(name, ‘ works in ‘, dept,’ department ’); LOWER()/ LCASE() Return string in small letters Select lower(‘INDIA’); Output- india Select lower(name) from emp; SUBSTRING(S, P,N) / MID(S,P,N) Return N character of string S, beginning from P Select SUBSTRING(‘LAPTOP’,3,3); Output – PTO Select SUBSTR(‘COMPUTER’,4,3); Output – PUT UPPER()/ UCASE() Return string in capital letters Select Upper(‘india’); Output- INDIA LTRIM() Removes leading space Select LTRIM(‘ Apple’); Output- ‘Apple’ RTRIM Remove trailing space Select RTRIM(‘Apple ‘); Output- ‘Apple’ String Function
  • 17. Function Description Example TRIM() Remove spaces from beginning and ending Select TRIM(‘ Apple ‘); Output-’Apple’ Select* fromemp where trim(name) = ‘Suyash’; INSTR() It search one string in another string and returns position, if not found 0 SelectINSTR(‘COMPUTER’,’PUT’); Output-4 Select INSTR(‘PYTHON’,’C++’); Output – 0 LENGTH() Returns number of character in string Selectlength(‘python’); Output- 7 Select name, length(name) fromemp LEFT(S,N) Return N characters of S from beginning SelectLEFT(‘KV NO1 TEZPUR’,2); Output-KV String Function
  • 18. RIGHT(S,N) Return N characters of S from ending Select RIGHT(‘KV NO1 ’,3); Output- NO1
  • 19. Function Description Example MOD(M,N) Return remainderM/N SelectMOD(11,5); Output- 1 POWER(B,P) Return B to power P SelectPOWER(2,5); Output-32 ROUND(N,D) Return number rounded to D place after decimal SelectROUND(11.589,2); Output- 11.59 SelectROUND(12.999,2); Output- 13.00 SelectROUND(267.478,-2) OUTPUT- 300 SIGN(N) Return -1 for –ve number 1 for ve + number Selectsign(-10) Output : -1 Selectsign(10); Output : 1 SQRT(N) Returns square rootof N SelectSQRT(144);Output: 12 TRUNCATE(M, N) Return number upto N place after decimal without rounding it SelectTruncate(15.789,2);Output: 15.79 Numeric Function
  • 20. Function Description Example CURDATE()/ CURRENT_DATE()/ CURRENT_DATE Return the current date Selectcurdate(); Select current_date(); DATE() Return date part from datetime expression Select date(‘2018-08-15 12:30’); Output: 2018-08-15 MONTH() Return month fromdate Selectmonth(‘2018-08-15’);Output: 08 YEAR() Return year fromdate Selectyear(‘2018-08-15’);Output: 2018 DAYNAME() Return weekday name Select dayname(‘2018-12-04’); Output: Tuesday DAYOFMONTH() Return value from1-31 Select dayofmonth(‘2018-08-15’) Output: 15 DAYOFWEEK() Return weekday index, for Sunday-1, Monday-2, .. Select dayofweek(‘2018-12-04’); Output: 3 Date and Time Function
  • 21. DAYOFYEAR() Return value from1-366 Select dayofyear(‘2018-02-10’) Output: 41
  • 22. Date and Time Function Function Description Example NOW() Return both current date and time Select now(); at which the function executes SYSDATE() Return both current date and time Select sysdate() Difference Between NOW() and SYSDATE(): NOW() function return the date and time at which function was executed even if we execute multiple NOW() function with select. whereas SYSDATE() will always return date and time at which each SYDATE() function started execution. For example. mysql> Select now(), sleep(2), now(); Output: 2018-12-04 10:26:20, 0, 2018-12-04 10:26:20 mysql> Select sysdate(), sleep(2), sysdate(); Output: 2018-12-04 10:27:08, 0, 2018-12-04 10:27:10