UPDATE - UPDATING A TABLE
• Changes values in a table
Syntax:
UPDATE <table name>/<view name>
 SET <column name> = <new va...
Ex:
      UPDATE EMP
         SET salary = salary +1000
             WHERE Job = ‘Analyst’ ;


UPDATE EMP
         SET sal...
DELETE - DELETING ROWS FROM A TABLE
•Removes one or more rows from a table.


DELETE FROM <table name> [<alias name>]
 [<W...
DELETE FROM Employee     WHERE name =
'Mihir';
DELETE * FROM Employee;
 DELETE FROM Employee WHERE name NOT
IN (‘Tony’, ‘T...
ALTER TABLE - ADDING ATTRIBUTES TO A
TABLE
•Adds new columns to a table.


ALTER TABLE <table name>
 ADD (<column name> <d...
ALTER TABLE staff
    drop column phone;



ALTER TABLE staff
    modify name varchar2(40);




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


DROP TABLE <table name>;


When you DROP...
CREATE VIEW – CREATING A VIEW
Defines a view that combines data from one or
more tables/views.
A column list must be speci...
Ex:
CREATE VIEW emp AS SELECT * FROM
   Employee
      WHERE dept = 'SOFTWARE';




                                 MGCL1...
DROP VIEW – REMOVING A VIEW
Removes specified view from the active database.
      DROP VIEW <view name>;
When a view is D...
Aggregate functions
Aggregate functions work with a group of values and
reduce them to a single value.
COUNT
• counts rows...
•If used in the SELECT clause, all other columns
SELECTed must also be SQL aggregate functions or
columns specified in a G...
MAX and MIN
• Used in SELECT or HAVING clauses
• MAX() function returns the highest value in the specified
column or colum...
SUM
• Used in the SELECT or HAVING clauses to find the
sum of values for the specified column.
• ALL is the default.
• DIS...
AVG
Used in the SELECT or HAVING clause to find the
average value for the specified column or column
expression.
ALL is th...
AGGREGATE CLAUSES : GROUP BY AND
HAVING
GROUP BY clause
• Is used to divide the rows in a table into smaller groups
• Grou...
GROUP BY <column name>[,<column name>...]
• Group functions (AVG, MAX, MIN, SUM, COUNT)
can be used with group by clause t...
• Groups rows together that have duplicate values for the
specified column
• SQL aggregate functions (AVG, MAX, MIN, SUM, ...
SELECT Class, COUNT(Sname) FROM Student GROUP
BY Class;
SELECT Dept, COUNT(ename) FROM Emp GROUP
BY dept;
SELECT Class, AV...
HAVING clause
• Used to restrict groups returned from GROUP BY
clause
• places condition on groups
• can include aggregate...
SELECT dept, MAX(salary) FROM Emp
     GROUP BY dept
           HAVING COUNT(*) > 10;


SELECT job_code,avg(salary), sum(s...
Upcoming SlideShare
Loading in …5
×

Updat Dir

254
-1

Published on

For NPS CLASS 12

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
254
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Updat Dir

  1. 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. 2. Ex: UPDATE EMP SET salary = salary +1000 WHERE Job = ‘Analyst’ ; UPDATE EMP SET salary = salary +1000, comm = 5000 WHERE Job = ‘SALESMAN’ ; MGCL12SQL4
  3. 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. 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. 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. 6. ALTER TABLE staff drop column phone; ALTER TABLE staff modify name varchar2(40); MGCL12SQL4
  7. 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. 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. 9. Ex: CREATE VIEW emp AS SELECT * FROM Employee WHERE dept = 'SOFTWARE'; MGCL12SQL4
  10. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×