SlideShare a Scribd company logo
SQL Commands
 Create a database:
 Open a database:
 Delete a database
Create table Command
 It is used to create a table in a database
 E.g
Insert a Record
 Insert command is used to insert a record
 Insert a row in table
 Insert null values in the table
 Insert values in particular column
Display structure of table
 Describe <table name> or desc <tablename>
Update Command
 It is used to change the record of the table. You can change all the rows or
particular row from the table. The Update command specifies the row with
where clause and new data is written into respective record using set
keyword. E.g
 Student table before change
Various way of Updating records
 Change a particular value
 Updating multiple records
 Updating Null value
 Updating using expression
Records after Modification
Delete Records
 There are two commands to delete
records:
 Delete
 Truncate
 Delete:it is used to delete all
rows or particular row from the
table using where clause.
 Truncate: to delete al the row
from table and free the space for
containing the table
 Drop Table: This command is used
to physically delete the table i.e.
remove structure also.
 E.g. of delete
 E.g. of truncate
 E.g. Drop table
Modify the Structure of Table
 Alter Table command is used to modify
the structure of table by modifying the
column definition of its column. It
perform following operations:
 To add new column in table
 To rename column
 To change data type or modify size
 To remove a column physically
Structure after Modification
Before Modification After Modification
SQL Commands
Grouping Records in a Query
•Some time it is required to apply a Select query in a group of
records instead of whole table.
•We can group records by using GROUP BY <column> clause with
Select command. A group column is chosen which have non-
distinct (repeating) values like City, Job etc.
• Generally, the following Aggregate Functions [MIN(), MAX(),
SUM(), AVG(), COUNT()] etc. are applied on groups.
Name Purpose
SUM() Returns the sum of given column.
MIN() Returns the minimum value in the given column.
MAX() Returns the maximum value in the given column.
AVG() Returns the Average value of the given column.
COUNT() Returns the total number of values/ records as per given
column.
SQL Commands
Aggregate Functions & NULL
Consider a table Emp having following records as-
Null values are excluded while (avg)aggregate function is
used
SQL Queries
mysql> Select Sum(Sal) from EMP;
mysql> Select Min(Sal) from EMP;
mysql> Select Max(Sal) from EMP;
mysql> Select Count(Sal) from
EMP; mysql> Select Avg(Sal) from
EMP; mysql> Select Count(*) from
EMP;
Emp
Code Name Sal
E1 Mohak NULL
E2 Anuj 4500
E3 Vijay NULL
E4 Vishal 3500
E5 Anil 4000
Result of query
12000
3500
4500
3
4000
5
SQL Commands
Aggregate Functions & Group
An Aggregate function may applied on a column with DISTINCT or
ALL keyword. If nothing is given ALL is assumed.
Using SUM (<Column>)
This function returns the sum of values in given column orexpression.
mysql> Select Sum(Sal) from EMP;
mysql> Select Sum(DISTINCT Sal) from EMP;
mysql> Select Sum ( S a l ) from EMP where
City=‘Jaipur ’;
mysql> Select Sum ( S a l ) from EMP Group By C i t y ;
mysql> Select Job, Sum(Sal) from EMP Group By Job;
Using MIN (<column>)
This functions returns the Minimum value in the given column.
mysql> Select Min(Sal) from EMP;
mysql> Select Min(Sal) from EMP Group By C i t y ;
mysql> Select Job, Min(Sal) from EMP Group By Job;
SQL Commands
 Using AVG (<column>)
 This functions returns the Average value in the given column.
 Using COUNT (<*|column>)
 This functions returns the number of rows in the given column.
Aggregate Functions & Group
Using MAX (<Column>)
This function returns the Maximum value in given column.
mysql> Select AVG(Sal) from EMP;
mysql> Select AVG(Sal) from EMP Group By C i t
y ;
mysql> Select Max(Sal) from EMP;
mysql> Select Max(Sal) from EMP where City=‘Jaipur ’;
mysql> Select Max(Sal) from EMP Group By C i t y ;
mysql> Select Count ( * ) from EMP;
mysql> Select Count(Sal) from EMP Group By C i t y ;
mysql> Select Count(*), Sum(Sal) from EMP Group By Job;
SQL Commands
Aggregate Functions & Conditions
You may use any condition on group, if required. HAVING
<condition> clause is used to apply a condition on a group.
mysql> Select Job,Sum(Pay) from EMP
Group By Job HAVING Sum(Pay)>=8000;
mysql> Select Job, Sum(Pay) from EMP
Group By Job HAVING Avg(Pay)>=7000;
mysql> Select Job, Sum(Pay) from EMP Group By Job HAVING
Count(*)>=5;
mysql> Select Job, Min(Pay),Max(Pay), Avg(Pay) from EMP Group
By Job HAVING Sum(Pay)>=8000;
mysql> Select Job, Sum(Pay) from EMP Where City=‘Jaipur’
Note :- Where clause works in respect of whole table but Having works on
Group only. If Where and Having both are used then Where will be
executed first.
SQL Commands
Ordering Query Result – ORDER BY Clause
A query result can be orders in ascending (A-Z) or
descending (Z-A)
order as per any column. Default is Ascending order.
mysql> SELECT * FROM Student ORDER BY City;
To get descending order use DESC key word.
mysql> SELECT * FROM Student ORDER BY City
DESC;
mysql> SELECT Name, Fname, City FROM Student
Where Name LIKE ‘R%’ ORDER BY Class;
Ad

More Related Content

Similar to MySQL-commands.pdf (20)

Sql functions
Sql functionsSql functions
Sql functions
ilias ahmed
 
Babitha2.mysql
Babitha2.mysqlBabitha2.mysql
Babitha2.mysql
banubabitha
 
Babitha2 Mysql
Babitha2 MysqlBabitha2 Mysql
Babitha2 Mysql
banubabitha
 
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
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
SANTOSH RATH
 
Module03
Module03Module03
Module03
Sridhar P
 
Sql
SqlSql
Sql
Karunakaran Mallikeswaran
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
Dr. C.V. Suresh Babu
 
V22 function-1
V22 function-1V22 function-1
V22 function-1
Dhirendra Chauhan
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
Huda Alameen
 
Mandatory sql functions for beginners
Mandatory sql functions for beginnersMandatory sql functions for beginners
Mandatory sql functions for beginners
shravan kumar chelika
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
Dr. C.V. Suresh Babu
 
ADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptx
ArjayBalberan1
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
poornima sugumaran
 
SQL Class Note By Amit Maity PowerPoint Presentation
SQL Class Note By Amit Maity PowerPoint PresentationSQL Class Note By Amit Maity PowerPoint Presentation
SQL Class Note By Amit Maity PowerPoint Presentation
maitypradip938
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
poornima sugumaran
 
My SQL.pptx
My SQL.pptxMy SQL.pptx
My SQL.pptx
KieveBarreto1
 
Lab3 aggregating data
Lab3   aggregating dataLab3   aggregating data
Lab3 aggregating data
Balqees Al.Mubarak
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
Sachin Shukla
 
Updat Dir
Updat DirUpdat Dir
Updat Dir
guest319770
 

Recently uploaded (20)

Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
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
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
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
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
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.
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
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
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
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
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Ad

MySQL-commands.pdf

  • 1. SQL Commands  Create a database:  Open a database:  Delete a database
  • 2. Create table Command  It is used to create a table in a database  E.g
  • 3. Insert a Record  Insert command is used to insert a record  Insert a row in table  Insert null values in the table  Insert values in particular column
  • 4. Display structure of table  Describe <table name> or desc <tablename>
  • 5. Update Command  It is used to change the record of the table. You can change all the rows or particular row from the table. The Update command specifies the row with where clause and new data is written into respective record using set keyword. E.g  Student table before change
  • 6. Various way of Updating records  Change a particular value  Updating multiple records  Updating Null value  Updating using expression
  • 8. Delete Records  There are two commands to delete records:  Delete  Truncate  Delete:it is used to delete all rows or particular row from the table using where clause.  Truncate: to delete al the row from table and free the space for containing the table  Drop Table: This command is used to physically delete the table i.e. remove structure also.  E.g. of delete  E.g. of truncate  E.g. Drop table
  • 9. Modify the Structure of Table  Alter Table command is used to modify the structure of table by modifying the column definition of its column. It perform following operations:  To add new column in table  To rename column  To change data type or modify size  To remove a column physically
  • 10. Structure after Modification Before Modification After Modification
  • 11. SQL Commands Grouping Records in a Query •Some time it is required to apply a Select query in a group of records instead of whole table. •We can group records by using GROUP BY <column> clause with Select command. A group column is chosen which have non- distinct (repeating) values like City, Job etc. • Generally, the following Aggregate Functions [MIN(), MAX(), SUM(), AVG(), COUNT()] etc. are applied on groups. Name Purpose SUM() Returns the sum of given column. MIN() Returns the minimum value in the given column. MAX() Returns the maximum value in the given column. AVG() Returns the Average value of the given column. COUNT() Returns the total number of values/ records as per given column.
  • 12. SQL Commands Aggregate Functions & NULL Consider a table Emp having following records as- Null values are excluded while (avg)aggregate function is used SQL Queries mysql> Select Sum(Sal) from EMP; mysql> Select Min(Sal) from EMP; mysql> Select Max(Sal) from EMP; mysql> Select Count(Sal) from EMP; mysql> Select Avg(Sal) from EMP; mysql> Select Count(*) from EMP; Emp Code Name Sal E1 Mohak NULL E2 Anuj 4500 E3 Vijay NULL E4 Vishal 3500 E5 Anil 4000 Result of query 12000 3500 4500 3 4000 5
  • 13. SQL Commands Aggregate Functions & Group An Aggregate function may applied on a column with DISTINCT or ALL keyword. If nothing is given ALL is assumed. Using SUM (<Column>) This function returns the sum of values in given column orexpression. mysql> Select Sum(Sal) from EMP; mysql> Select Sum(DISTINCT Sal) from EMP; mysql> Select Sum ( S a l ) from EMP where City=‘Jaipur ’; mysql> Select Sum ( S a l ) from EMP Group By C i t y ; mysql> Select Job, Sum(Sal) from EMP Group By Job; Using MIN (<column>) This functions returns the Minimum value in the given column. mysql> Select Min(Sal) from EMP; mysql> Select Min(Sal) from EMP Group By C i t y ; mysql> Select Job, Min(Sal) from EMP Group By Job;
  • 14. SQL Commands  Using AVG (<column>)  This functions returns the Average value in the given column.  Using COUNT (<*|column>)  This functions returns the number of rows in the given column. Aggregate Functions & Group Using MAX (<Column>) This function returns the Maximum value in given column. mysql> Select AVG(Sal) from EMP; mysql> Select AVG(Sal) from EMP Group By C i t y ; mysql> Select Max(Sal) from EMP; mysql> Select Max(Sal) from EMP where City=‘Jaipur ’; mysql> Select Max(Sal) from EMP Group By C i t y ; mysql> Select Count ( * ) from EMP; mysql> Select Count(Sal) from EMP Group By C i t y ; mysql> Select Count(*), Sum(Sal) from EMP Group By Job;
  • 15. SQL Commands Aggregate Functions & Conditions You may use any condition on group, if required. HAVING <condition> clause is used to apply a condition on a group. mysql> Select Job,Sum(Pay) from EMP Group By Job HAVING Sum(Pay)>=8000; mysql> Select Job, Sum(Pay) from EMP Group By Job HAVING Avg(Pay)>=7000; mysql> Select Job, Sum(Pay) from EMP Group By Job HAVING Count(*)>=5; mysql> Select Job, Min(Pay),Max(Pay), Avg(Pay) from EMP Group By Job HAVING Sum(Pay)>=8000; mysql> Select Job, Sum(Pay) from EMP Where City=‘Jaipur’ Note :- Where clause works in respect of whole table but Having works on Group only. If Where and Having both are used then Where will be executed first.
  • 16. SQL Commands Ordering Query Result – ORDER BY Clause A query result can be orders in ascending (A-Z) or descending (Z-A) order as per any column. Default is Ascending order. mysql> SELECT * FROM Student ORDER BY City; To get descending order use DESC key word. mysql> SELECT * FROM Student ORDER BY City DESC; mysql> SELECT Name, Fname, City FROM Student Where Name LIKE ‘R%’ ORDER BY Class;