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

Updat Dir

  • 1.
    UPDATE - UPDATINGA 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 - DELETINGROWS 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 empAS 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 functionswork 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 inthe 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 inthe 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 theSELECT 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 <columnname>[,<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 rowstogether 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 • Usedto 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