Upload
guest319770
View
235
Download
0
Tags:
Embed Size (px)
DESCRIPTION
For NPS CLASS 12
Citation preview
MG\CL12\SQL4
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.
MG\CL12\SQL4
Ex:
UPDATE EMP
SET salary = salary +1000
WHERE Job = ‘Analyst’ ;
UPDATE EMP
SET salary = salary +1000,
comm = 5000
WHERE Job = ‘SALESMAN’ ;
MG\CL12\SQL4
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.
MG\CL12\SQL4
DELETE FROM Employee WHERE name = 'Mihir';
DELETE * FROM Employee;
DELETE FROM Employee WHERE name NOT IN (‘Tony’, ‘Tom’);
DELETE FROM Employee WHERE salary < 10000;
MG\CL12\SQL4
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));
MG\CL12\SQL4
ALTER TABLE staffdrop column phone;
ALTER TABLE staffmodify name varchar2(40);
MG\CL12\SQL4
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;
MG\CL12\SQL4
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];
MG\CL12\SQL4
Ex:
CREATE VIEW emp AS SELECT * FROM Employee
WHERE dept = 'SOFTWARE';
MG\CL12\SQL4
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;
MG\CL12\SQL4
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
MG\CL12\SQL4
•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;
MG\CL12\SQL4
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;
MG\CL12\SQL4
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;
MG\CL12\SQL4
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;
MG\CL12\SQL4
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
MG\CL12\SQL4
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
MG\CL12\SQL4
• 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
MG\CL12\SQL4
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)
MG\CL12\SQL4
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
MG\CL12\SQL4
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;