SlideShare une entreprise Scribd logo
1  sur  21
Télécharger pour lire hors ligne
UPDATE - UPDATING A TABLE
• Changes values in a table
Syntax:
UPDATE <table name>/<view name>
 SET <column name> = <new value>
 [,<column name> = <new value>...]
 [<WHERE clause>];
• The WHERE clause specifies the rows to be UPDATEd

• Omitting this clause UPDATEs all rows in the table.
                                                    MGCL12SQL4
Ex:
      UPDATE EMP
         SET salary = salary +1000
             WHERE Job = ‘Analyst’ ;


UPDATE EMP
         SET salary = salary +1000,
          comm = 5000
             WHERE Job = ‘SALESMAN’ ;



                                        MGCL12SQL4
DELETE - DELETING ROWS FROM A TABLE
•Removes one or more rows from a table.


DELETE FROM <table name> [<alias name>]
 [<WHERE clause>];
• The WHERE clause specifies rows to be deleted.
• If the WHERE clause is not included, all rows will be
deleted.



                                                   MGCL12SQL4
DELETE FROM Employee     WHERE name =
'Mihir';
DELETE * FROM Employee;
 DELETE FROM Employee WHERE name NOT
IN (‘Tony’, ‘Tom’);
DELETE FROM Employee      WHERE salary <
10000;



                                    MGCL12SQL4
ALTER TABLE - ADDING ATTRIBUTES TO A
TABLE
•Adds new columns to a table.


ALTER TABLE <table name>
 ADD (<column name> <data type>
 [,<column name> <data type>...]);


Ex:   ALTER TABLE staff
      ADD (phone CHAR(13));
                                     MGCL12SQL4
ALTER TABLE staff
    drop column phone;



ALTER TABLE staff
    modify name varchar2(40);




                                MGCL12SQL4
DROP TABLE – REMOVING A TABLE
Removes specified table from the active database.


DROP TABLE <table name>;


When you DROP a table, all indexes, synonyms, and
views associated with it are DROPped.


Ex:   DROP TABLE emp;

                                                    MGCL12SQL4
CREATE VIEW – CREATING A VIEW
Defines a view that combines data from one or
more tables/views.
A column list must be specified if any of the
columns in the SELECT are calculated columns or
expressions, otherwise view columns inherit default
names from the base table(s)/view(s).
CREATE VIEW <view name> [(<column name>,
<column name>..)]
 AS <SELECT        command>      [WITH    CHECK
OPTION];
                                              MGCL12SQL4
Ex:
CREATE VIEW emp AS SELECT * FROM
   Employee
      WHERE dept = 'SOFTWARE';




                                 MGCL12SQL4
DROP VIEW – REMOVING A VIEW
Removes specified view from the active database.
      DROP VIEW <view name>;
When a view is DROPped, all other views and
synonyms based on it are dropped automatically.
When a table is dropped, all views based on it are also
dropped.
Ex: DROP VIEW empview;




                                                    MGCL12SQL4
Aggregate functions
Aggregate functions work with a group of values and
reduce them to a single value.
COUNT
• counts rows/records
• * can be used instead of ALL
      COUNT ({*/[DISTINCT] <column name>})
• Used in SELECT or HAVING clause to count the
number of rows returned by an SQL command
• DISTINCT omits any repeated values

                                              MGCL12SQL4
•If used in the SELECT clause, all other columns
SELECTed must also be SQL aggregate functions or
columns specified in a GROUP BY clause
Examples
SELECT COUNT (*)     FROM staff   WHERE salary >
15500;
SELECT COUNT (DISTINCT name) FROM Employee;
SELECT COUNT (*) FROM Employee;
 SELECT COUNT(ALL desig) from Employee where
salary >15500;


                                           MGCL12SQL4
MAX and MIN
• Used in SELECT or HAVING clauses
• MAX() function returns the highest value in the specified
column or column expression
• MIN() returns the lowest value
• If used in the SELECT clause, all other columns selected
must also be SQL aggregate functions or columns specified
in a GROUP BY clause.
{MAX/MIN} ([ALL/DISTINCT] <column name>)


SELECT MAX (salary), MIN (salary) FROM Emp;
                                                    MGCL12SQL4
SUM
• Used in the SELECT or HAVING clauses to find the
sum of values for the specified column.
• ALL is the default.
• DISTINCT omits any repeated values.
• If used in the SELECT clause, all other columns
SELECTed must also be SQL aggregate functions or
columns specified in a GROUP BY clause.
      SUM ([ALL/DISTINCT] <column name>)


SELECT SUM(salary) from Emp;
                                              MGCL12SQL4
AVG
Used in the SELECT or HAVING clause to find the
average value for the specified column or column
expression.
ALL is the default.
DISTINCT omits any repeated values.
If used in the SELECT clause, all other columns
SELECTed must also be SQL aggregate functions or
columns specified in a GROUP BY clause.
       AVG ([ALL/DISTINCT] <column name>)
SELECT AVG (Height) FROM Student;
                                            MGCL12SQL4
AGGREGATE CLAUSES : GROUP BY AND
HAVING
GROUP BY clause
• Is used to divide the rows in a table into smaller groups
• Grouping can be done by a column name, or with
aggregate function




                                                       MGCL12SQL4
GROUP BY <column name>[,<column name>...]
• Group functions (AVG, MAX, MIN, SUM, COUNT)
can be used with group by clause to return summary
information for each group
• Any non-aggregate function columns in a SELECT
clause that includes aggregate functions must be specified
in a GROUP BY clause
• You cannot GROUP BY columns of type LOGICAL




                                                     MGCL12SQL4
• Groups rows together that have duplicate values for the
specified column
• SQL aggregate functions (AVG, MAX, MIN, SUM, or
COUNT) in a SELECT clause operate on each group
• Any non-aggregate function columns in a SELECT
clause that includes aggregate functions must be specified
in a GROUP BY clause
• You cannot GROUP BY columns of type LOGICAL
• WHERE clause can be used to exclude rows before
forming groups


                                                     MGCL12SQL4
SELECT Class, COUNT(Sname) FROM Student GROUP
BY Class;
SELECT Dept, COUNT(ename) FROM Emp GROUP
BY dept;
SELECT Class, AVG(Height) FROM Student GROUP
BY Class;
SELECT Dept, MAX(salary) FROM Emp WHERE DOJ
> ’01-Jan-2005’ GROUP BY dept;
SELECT Class, Sec, COUNT(*) FROM Student GROUP
BY Class, Sec;
(Group all students by class, then within each class group
by sec)
                                                      MGCL12SQL4
HAVING clause
• Used to restrict groups returned from GROUP BY
clause
• places condition on groups
• can include aggregate functions
      HAVING [NOT]<search condition>
• In a query using GROUP BY and HAVING clause,
the rows are first grouped, group functions are applied
and then only those groups matching HAVING clause
are displayed


                                                 MGCL12SQL4
SELECT dept, MAX(salary) FROM Emp
     GROUP BY dept
           HAVING COUNT(*) > 10;


SELECT job_code,avg(salary), sum(salary)
from Emp
      GROUP BY job_code
           HAVING job_code=3;



                                           MGCL12SQL4

Contenu connexe

Tendances (17)

Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Oracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guideOracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guide
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Commands of DML in SQL
Commands of DML in SQLCommands of DML in SQL
Commands of DML in SQL
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
 
Null values, insert, delete and update in database
Null values, insert, delete and update in databaseNull values, insert, delete and update in database
Null values, insert, delete and update in database
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
SQL
SQLSQL
SQL
 
Sql DML
Sql DMLSql DML
Sql DML
 
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
Views, Triggers, Functions, Stored Procedures,  Indexing and JoinsViews, Triggers, Functions, Stored Procedures,  Indexing and Joins
Views, Triggers, Functions, Stored Procedures, Indexing and Joins
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 

En vedette (18)

Networking
NetworkingNetworking
Networking
 
Ashraya
AshrayaAshraya
Ashraya
 
Николай Коперник
Николай КоперникНиколай Коперник
Николай Коперник
 
Taller Dels Animalons
Taller Dels AnimalonsTaller Dels Animalons
Taller Dels Animalons
 
Gan La Taiwan Day5
Gan La Taiwan Day5Gan La Taiwan Day5
Gan La Taiwan Day5
 
Baby Sleeping Habits By Evebel
Baby Sleeping Habits By EvebelBaby Sleeping Habits By Evebel
Baby Sleeping Habits By Evebel
 
AIEDDs_Basic EOD
AIEDDs_Basic EODAIEDDs_Basic EOD
AIEDDs_Basic EOD
 
Telecomunications
TelecomunicationsTelecomunications
Telecomunications
 
NEW UNIFORM
NEW UNIFORMNEW UNIFORM
NEW UNIFORM
 
Fire Anniversary pdf
Fire Anniversary pdfFire Anniversary pdf
Fire Anniversary pdf
 
Kordamine kontrolltööks katoliku kirikust
Kordamine kontrolltööks katoliku kirikustKordamine kontrolltööks katoliku kirikust
Kordamine kontrolltööks katoliku kirikust
 
Using Facebook To Create Your Web Personality
Using Facebook To Create Your Web PersonalityUsing Facebook To Create Your Web Personality
Using Facebook To Create Your Web Personality
 
R2R Meeting 14 pdf
R2R Meeting 14 pdfR2R Meeting 14 pdf
R2R Meeting 14 pdf
 
cv 2 pdf henry
cv 2 pdf henrycv 2 pdf henry
cv 2 pdf henry
 
Mikayla And Kendra Gray
Mikayla And Kendra GrayMikayla And Kendra Gray
Mikayla And Kendra Gray
 
Trans Media Final
Trans Media FinalTrans Media Final
Trans Media Final
 
ПРОЕКТ «СТУДІЯ «НАШ ДОМ»
ПРОЕКТ «СТУДІЯ «НАШ ДОМ»ПРОЕКТ «СТУДІЯ «НАШ ДОМ»
ПРОЕКТ «СТУДІЯ «НАШ ДОМ»
 
Galbavy Koncesie
Galbavy KoncesieGalbavy Koncesie
Galbavy Koncesie
 

Similaire à Updat Dir

Similaire à Updat Dir (20)

Its about a sql topic for basic structured query language
Its about a sql topic for basic structured query languageIts about a sql topic for basic structured query language
Its about a sql topic for basic structured query language
 
Sql2
Sql2Sql2
Sql2
 
Introduction to oracle functions
Introduction to oracle functionsIntroduction to oracle functions
Introduction to oracle functions
 
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
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 
MySQL-commands.pdf
MySQL-commands.pdfMySQL-commands.pdf
MySQL-commands.pdf
 
Sql
SqlSql
Sql
 
Sql query [select, sub] 4
Sql query [select, sub] 4Sql query [select, sub] 4
Sql query [select, sub] 4
 
Basic SQL Statments
Basic SQL StatmentsBasic SQL Statments
Basic SQL Statments
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
SQL : Structured Query Language
SQL : Structured Query LanguageSQL : Structured Query Language
SQL : Structured Query Language
 
Sql
SqlSql
Sql
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
Interacting with Oracle Database
Interacting with Oracle DatabaseInteracting with Oracle Database
Interacting with Oracle Database
 
Les01
Les01Les01
Les01
 
SQL Query
SQL QuerySQL Query
SQL Query
 
Sql server ___________session 3(sql 2008)
Sql server  ___________session 3(sql 2008)Sql server  ___________session 3(sql 2008)
Sql server ___________session 3(sql 2008)
 

Dernier

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Dernier (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Updat Dir

  • 1. UPDATE - UPDATING A TABLE • Changes values in a table Syntax: UPDATE <table name>/<view name> SET <column name> = <new value> [,<column name> = <new value>...] [<WHERE clause>]; • The WHERE clause specifies the rows to be UPDATEd • Omitting this clause UPDATEs all rows in the table. MGCL12SQL4
  • 2. Ex: UPDATE EMP SET salary = salary +1000 WHERE Job = ‘Analyst’ ; UPDATE EMP SET salary = salary +1000, comm = 5000 WHERE Job = ‘SALESMAN’ ; MGCL12SQL4
  • 3. DELETE - DELETING ROWS FROM A TABLE •Removes one or more rows from a table. DELETE FROM <table name> [<alias name>] [<WHERE clause>]; • The WHERE clause specifies rows to be deleted. • If the WHERE clause is not included, all rows will be deleted. MGCL12SQL4
  • 4. DELETE FROM Employee WHERE name = 'Mihir'; DELETE * FROM Employee; DELETE FROM Employee WHERE name NOT IN (‘Tony’, ‘Tom’); DELETE FROM Employee WHERE salary < 10000; MGCL12SQL4
  • 5. ALTER TABLE - ADDING ATTRIBUTES TO A TABLE •Adds new columns to a table. ALTER TABLE <table name> ADD (<column name> <data type> [,<column name> <data type>...]); Ex: ALTER TABLE staff ADD (phone CHAR(13)); MGCL12SQL4
  • 6. ALTER TABLE staff drop column phone; ALTER TABLE staff modify name varchar2(40); MGCL12SQL4
  • 7. DROP TABLE – REMOVING A TABLE Removes specified table from the active database. DROP TABLE <table name>; When you DROP a table, all indexes, synonyms, and views associated with it are DROPped. Ex: DROP TABLE emp; MGCL12SQL4
  • 8. CREATE VIEW – CREATING A VIEW Defines a view that combines data from one or more tables/views. A column list must be specified if any of the columns in the SELECT are calculated columns or expressions, otherwise view columns inherit default names from the base table(s)/view(s). CREATE VIEW <view name> [(<column name>, <column name>..)] AS <SELECT command> [WITH CHECK OPTION]; MGCL12SQL4
  • 9. Ex: CREATE VIEW emp AS SELECT * FROM Employee WHERE dept = 'SOFTWARE'; MGCL12SQL4
  • 10. DROP VIEW – REMOVING A VIEW Removes specified view from the active database. DROP VIEW <view name>; When a view is DROPped, all other views and synonyms based on it are dropped automatically. When a table is dropped, all views based on it are also dropped. Ex: DROP VIEW empview; MGCL12SQL4
  • 11. Aggregate functions Aggregate functions work with a group of values and reduce them to a single value. COUNT • counts rows/records • * can be used instead of ALL COUNT ({*/[DISTINCT] <column name>}) • Used in SELECT or HAVING clause to count the number of rows returned by an SQL command • DISTINCT omits any repeated values MGCL12SQL4
  • 12. •If used in the SELECT clause, all other columns SELECTed must also be SQL aggregate functions or columns specified in a GROUP BY clause Examples SELECT COUNT (*) FROM staff WHERE salary > 15500; SELECT COUNT (DISTINCT name) FROM Employee; SELECT COUNT (*) FROM Employee; SELECT COUNT(ALL desig) from Employee where salary >15500; MGCL12SQL4
  • 13. MAX and MIN • Used in SELECT or HAVING clauses • MAX() function returns the highest value in the specified column or column expression • MIN() returns the lowest value • If used in the SELECT clause, all other columns selected must also be SQL aggregate functions or columns specified in a GROUP BY clause. {MAX/MIN} ([ALL/DISTINCT] <column name>) SELECT MAX (salary), MIN (salary) FROM Emp; MGCL12SQL4
  • 14. SUM • Used in the SELECT or HAVING clauses to find the sum of values for the specified column. • ALL is the default. • DISTINCT omits any repeated values. • If used in the SELECT clause, all other columns SELECTed must also be SQL aggregate functions or columns specified in a GROUP BY clause. SUM ([ALL/DISTINCT] <column name>) SELECT SUM(salary) from Emp; MGCL12SQL4
  • 15. AVG Used in the SELECT or HAVING clause to find the average value for the specified column or column expression. ALL is the default. DISTINCT omits any repeated values. If used in the SELECT clause, all other columns SELECTed must also be SQL aggregate functions or columns specified in a GROUP BY clause. AVG ([ALL/DISTINCT] <column name>) SELECT AVG (Height) FROM Student; MGCL12SQL4
  • 16. AGGREGATE CLAUSES : GROUP BY AND HAVING GROUP BY clause • Is used to divide the rows in a table into smaller groups • Grouping can be done by a column name, or with aggregate function MGCL12SQL4
  • 17. GROUP BY <column name>[,<column name>...] • Group functions (AVG, MAX, MIN, SUM, COUNT) can be used with group by clause to return summary information for each group • Any non-aggregate function columns in a SELECT clause that includes aggregate functions must be specified in a GROUP BY clause • You cannot GROUP BY columns of type LOGICAL MGCL12SQL4
  • 18. • Groups rows together that have duplicate values for the specified column • SQL aggregate functions (AVG, MAX, MIN, SUM, or COUNT) in a SELECT clause operate on each group • Any non-aggregate function columns in a SELECT clause that includes aggregate functions must be specified in a GROUP BY clause • You cannot GROUP BY columns of type LOGICAL • WHERE clause can be used to exclude rows before forming groups MGCL12SQL4
  • 19. SELECT Class, COUNT(Sname) FROM Student GROUP BY Class; SELECT Dept, COUNT(ename) FROM Emp GROUP BY dept; SELECT Class, AVG(Height) FROM Student GROUP BY Class; SELECT Dept, MAX(salary) FROM Emp WHERE DOJ > ’01-Jan-2005’ GROUP BY dept; SELECT Class, Sec, COUNT(*) FROM Student GROUP BY Class, Sec; (Group all students by class, then within each class group by sec) MGCL12SQL4
  • 20. HAVING clause • Used to restrict groups returned from GROUP BY clause • places condition on groups • can include aggregate functions HAVING [NOT]<search condition> • In a query using GROUP BY and HAVING clause, the rows are first grouped, group functions are applied and then only those groups matching HAVING clause are displayed MGCL12SQL4
  • 21. SELECT dept, MAX(salary) FROM Emp GROUP BY dept HAVING COUNT(*) > 10; SELECT job_code,avg(salary), sum(salary) from Emp GROUP BY job_code HAVING job_code=3; MGCL12SQL4