You are on page 1of 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

1.Database Concepts
Objective
Introduction
Database Management System
Traditional Data Processing System
Need of a Database Management System
Types of DBMS
Relational Database Management System Terminology
Keys

1.1

Introduction

1.2

Types of DBMS

1.3

RDBMS terms

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/database-concepts-0

1 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

1.1. Introduction
What is a data
Any factual information in raw and disorganized form is called data.
What is a database
A database is a collection of related data organized in a way that data can be easily accessed ,managed and updated.
Database Management System
A general-purpose Database Management System(DBMS) is a software system designed to allow the definition, creation, querying, updating
and administration of databases.
Well-known DBMS's include MySql,Oracle,Microsoft SQL server,Foxpro,SQLite, FileMaker Pro.
The primary goal of a Database Management System(DBMS) is to provide a way to store and retrieve database information that is both
convenient and efficient.
Traditional File Processing System
A file system is a method of storing and organizing computer files and the data they contain and to make it easy to find and access them.
Limitations of File processing System:
1. Separated and Isolated Data
2. Data Redundancy
3. Data Dependence
4. Difficulty in representing data from the user's view
5. Inflexibility in retrieving the Data
https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/introduction-20

2 / 56

Campus Commune

8/19/2014

6. Data Security
7. Transactional Problems
8. Concurrency problems
Need of a Database Management System
To overcome the limitations of the traditional file processing system , the modern DBMS was created.
Functions of DBMS :
1. Minimal Data Redundancy
2. Data Consistency
3. Data Integration
4. Data Sharing
5. Application Development Ease
6. Better Controls
7. Data Independence
8. Reduced Maintenance
Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/introduction-20

3 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

1.2. Types of DBMS


Commonly used databases are:
1. Hierarchical Database
2. Network model database
3. Object relational database
4. Relational database Model
Hierarchical DBMS:
1. A DBMS is said to be hierarchical if the relationships among data in the database are established in such a way that one data item is present
as the subordinate of another one or a sub unit.
2. The data structure "tree" is followed by the DBMS to structure the database.

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/types-of-dbms-0

4 / 56

Campus Commune

8/19/2014

Network Model DBMS:


1. A DBMS is said to be a Network DBMS if the relationships among data in the database are of type many-to-many and appears in the form of
a network.
2. Thus the structure of a network database is extremely complicated because of these many-to-many relationships in which one record can be
used as a key of the entire database.

Object Oriented Relational Database :


1. Object Oriented DBMS add database functionality to object oriented programming languages.
2. They bring much more than persistent storage of programming language objects.
3. As a result, applications require less code, use more natural data modelling, and code bases are easier to maintain.

Relational Database Management System:


1. A Data Base Management System that is based on a relational model is called as Relational Database Management System.
2. Relational model is the most successfully used Data Base Management System Model (DBMS) model.
3. Relational model represents data in the form of tables.
4. Some popular examples are Oracle, Microsoft SQL Server and Microsoft Access.

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/types-of-dbms-0

5 / 56

Campus Commune

Ask a doubt

8/19/2014

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/types-of-dbms-0

6 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

1.3. RDBMS terms


Relation:
The Relation is a collection of related data entries and it consists of columns and rows.
Tuple:
A tuple, also called a row of data, is each individual entry that exists in a table.
Fields:
Every table is broken up into smaller entities called fields.
A field is a column in a table that is designed to maintain specific information about every record in the table.

Keys in DBMS
Keys are used to establish and identify relation between tables
Super Key:
A Super key is any combination of fields within a table that uniquely identifies each record within that table.
Candidate Key:
1. A candidate is a subset of a super key.
2. A candidate key is a single field or the least combination of fields that uniquely identifies each record in the table.
3. The least combination of fields distinguishes a candidate key from a super key.
Primary key:
1. A primary key is a candidate key that is most appropriate to be the main reference key for the table.

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/rdbms-terms-0

7 / 56

Campus Commune

8/19/2014

2. It is a key that uniquely identifies a record in a table.


Foreign key:
1. A foreign key is the field that generally references values from primary key field of other table

Related Videos

Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

Open Doubts

Closed Doubts

BHARATHWAJ SUNDARESAN
The above material is totally not related to the questions on the quiz. and there are a lot of bugs. kindly do the needful.
about 1 month ago

SANTHOSH DEVUNURI
Hi
It would be great, if you can provide the details of the bugs which you found.
Thanks

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/rdbms-terms-0

8 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

2.Data Query Language


Objective
Introduction to SQL
Data Query Language (DQL) : SELECT
DQL : Projection
DQL : Selection
Using DISTINCT Keyword
Using ORDER BY Keyword
Using Concatenation Operator
Using Column Alias
2.1

Introduction to SQL

2.2

Data Query Language : SELECT

2.3

SELECT Statement (Projection & Selection)

2.4

DISTINCT Keyword & GROUP -ORDER BY Clause

2.5

Concatenation Operator & Column Alias

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-query-language-0

9 / 56

Campus Commune

2.6

8/19/2014

Practice problems

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-query-language-0

10 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

2.1. Introduction to SQL


Structured Query Language or SQL is a tool for organizing, managing, and retrieving data stored in a computer database.
SQL Statements can be divided into following sub categories:
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Query Language (DQL)
Data Control Language (DCL)
Transaction Control Language (TCL)
Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/introduction-to-sql-0

11 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

2.2. Data Query Language : SELECT


The Data Query Language (DQL) is used to retrieve the data from one or more tables.
The SELECT statement is the only DQL statement.
Using SELECT statement the following operation can be performed
Projection : The operation results subset of columns
Selection : The operation results subset of rows
Projection means selecting set of columns from a table. In the below table, 3 columns marked with yellow colour suggesting that those columns are
selected.

Selection means selecting set of rows from a table. In the below table, 3 rows marked with yellow colour suggesting that those rows are selected.

A Typical SELECT statement may consists of six clauses.


Syntax:
SELECT column_name [, column_list]
FROM table_name
https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-query-language-select-0

12 / 56

Campus Commune

8/19/2014

[WHERE search_condition]
[GROUP BY grouping_column_name]
[HAVING aggregated_search_condition]
[ORDER BY sorting_column]
1. The SELECT and FROM clauses of the statement are mandatory. The remaining four clauses are optional.
Consider the following table

Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-query-language-select-0

13 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

2.3. SELECT Statement (Projection & Selection)


To display all records from table SELECT keyword with asterisk(*) is used:

The above query displays all the records from customer_details


To retrieve selected columns from the table, specific column names separated by commas(,) are used

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/select-statement-projection-and-selection

14 / 56

Campus Commune

8/19/2014

The above query displays only id, name and salary columns from customer_details

SELECT Statement (selection)


The where clause can be used to refine selection results by adding a search criteria.
Restricting Selection using comparison operators:
SELECT name, age FROM customer_details WHERE salary>50000;
The above query displays name and age of customers whose salary more than 50000 From customer_details table
Restricting Selection using IN keyword:
SELECT name,location FROM customer_details WHERE location IN ('Kolkata','Delhi');
The above query displays name and location of customers who are from Kolkata and Delhi
Restrict based on pattern matching:
SELECT name,location FROM customer_details WHERE name LIKE '_a%';
The above query displays name and location of customers whose name's second character is a.
'_' identifies that one character can appear before 'a' and ' % ' identifies that any numbers of character can appear after 'a'.
Restricting Selection using BETWEEN AND keyword:
SELECT name,location FROM customer_details WHERE age BETWEEN 20 AND 40;
The above query displays name and location of customers whose age ranging from 20 to 40
Restricting based on NULL value:
In Database terminology NULL is referred as value which is unspecified or unavailable
Based on database configuration, NULL value may not display anything but it should not be misunderstood as zero or blank space.
Condition on NULL value can be given using the keyword IS.
SELECT name, age FROM customer_details WHERE location IS NULL;
The above query displays name and age of customers whose location not specified
SELECT name, age FROM customer_details WHERE age IS NOT NULL;
The above query displays name and age of customers whose age available

Related Videos

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/select-statement-projection-and-selection

15 / 56

Campus Commune

Ask a doubt

8/19/2014

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/select-statement-projection-and-selection

16 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

2.4. DISTINCT Keyword & GROUP -ORDER BY Clause


To eliminate duplicate rows DISTINCT keyword is used in the SELECT statement.

The above query displays only the different age values.


ORDER BY Clause
ORDER BY clause in the SELECT statement is used to sort the results.

1. Name, age and salary of the customers displayed as per the ascending order of their salary.
2. DESC keyword can be used to display the list in descending order
GROUP BY Clause

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/distinct-keyword-and-order-by-clause

17 / 56

Campus Commune

8/19/2014

The GROUP BY statement is used to group the result-set by one or more columns.

The above query displays number of customers present in particular age group.
Note: Group By Statements will be discussed in detail later
Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/distinct-keyword-and-order-by-clause

18 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

2.5. Concatenation Operator & Column Alias


The concatenation( || ) operator can be used to combine character strings and values from table.

The Name, age and the strings are displayed as single output columns
Column Alias
Column heading in the result set can be changed with Column aliases using the keyword AS.

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/concatenation-operator-and-column-alias

19 / 56

Campus Commune

8/19/2014

1. The columns name and id are displayed as customer_name and customer_id in the result set.
2. Column headings in the table is unchanged.

Related Videos

Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/concatenation-operator-and-column-alias

20 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

2.6. Practice problems


Note
Q1 Q2
Q3
Q6 Q9
Q10

: Use the table PRODUCT prod_code,prod_name,price,manufacturer


: Use the table BOX - box_code,contents,value,warehouse
: Use the table EMPLOYEEempid,firstname,lastname,salary,department
: Use the table SWIPES-empid,shiftcode,swipedate,intime,outtime

1. Write a statement to select all the products with a price between $60 and $120.
2. Write a statement to select the name price in dollor and price
in cents(use alias)(1$=100 cents)
3. Write a statement to get different contents in boxes
4. Write a statement to get the full name of employee by combining firstname and lastname
5. Write a query to get the employees who are not assigned to any department?
6. Write a query to get employees salary is less than 7000 and belongs to department 20 or 40
7. Write a query to get the employes who are having managers.
8. Write a query to display the details of all employees whose last name is not available.
9. Write a query to display the details of all the female employees who is earning more than 12000 an belongs to the designation 'A'.
10. Write a query to display the various shift codes available in the SWIPES table.

Click here to download the solutions


Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/practice-problems-9

21 / 56

Campus Commune

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/practice-problems-9

8/19/2014

22 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

3.Data Definition Language


Objective
Introduction to DDL
Data types
Types of Data Definition Language
3.1

Data Definition Language (DDL)

3.2

Practice problems

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-definition-language-1

23 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

3.1. Data Definition Language (DDL)


Data Definition Language (DDL)
DDL statements define the data structure such as tables, views etc.. that make up the database. All DDL statements are auto committed means the
changes will become permanent and database objects created are available to all users.
Commonly used DDL statements are:
CREATE
ALTER
DROP
RENAME
TRUNCATE

Data Types
A data type identifies or classifies a particular type of information or data.
Some commonly used data types are:
CHAR (size) - Used to store character strings values of fixed length.
VARCHAR2 (size) Used to store variable length string data.
NUMBER (size, precision) Used to store numbers(fixed or floating point)
DATE Used to represent date and time.
LONG Used to store large variable length strings(upto 2GB).

DDL statement: CREATE


The CREATE keyword is used for creating database objects like tables, views, triggers, and indexes.
Syntax:
CREATE TABLE table_name

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-definition-language-ddl-0

24 / 56

Campus Commune

8/19/2014

(
column_name1 DATATYPE(Size) ,
column_name2 DATATYPE(Size),
column_name3 DATATYPE(Size)
);
Ex: Create Table Employee
(
Emp_id number(4) NOT NULL,
Name varchar2(20) NOT NULL,
Salary number(8),
E_Mail varchar2(30),
Country varchar2(20),
);
Table created.
The above statement creates a table named Employee with columns Emp_id, Name,Salary,E_Mail and Country.

DDL statement: ALTER


Alter statement is used to modify the structure of database or database objects like tables, views etc..
Alter table statement - to add column to a table:

The above statement adds a new column 'age' of number data type with constraint not null.
Modifying the column:
Ex: ALTER TABLE Employee MODIFY salary number(10,2);
Table Altered.
Rename and Drop a Column:
Using the alter statement we can rename a column and also the drop any column.

The above statements renames the column name salary to em_sal and drops column age.

DDL statements: TRUNCATE, DROP, RENAME


Truncate Table:

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-definition-language-ddl-0

25 / 56

Campus Commune

8/19/2014

Remove all the rows and resets schema of the table.


TRUNCATE TABLE Employee;
Drop table :
Deletes table entirely from the database.
DROP TABLE Employee;
Renaming a Table:
RENAME Employee to Emp_table;

Related Videos

Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-definition-language-ddl-0

26 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

3.2. Practice problems


Note
Q1 Q4 : Use the table PRODUCT prod_code,prod_name,price,manufacturer
Q5 : Use the table BOX - box_code,contents,value,warehouse
Q6 Q7 : Use the table EMPLOYEEempid,firstname,lastname,salary,department
Q9 : Use the table SWIPES-empid,shiftcode,swipedate,intime,outtime
Q10-Q12 : Use the table EMPLOYEE
Q13 : Use the table SWIPES

1. Create table statement for products table

2. Create a products_copy table with the products having code 1 to 5

3. Write a query to alter the name column in products table to varchar2(35) to varchar2(80)

4. Write a statement to empty the products table

5. Write a statement to delete the box table from tablespace?

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/practice-problems-10

27 / 56

Campus Commune

8/19/2014

6. Write a query to prepare a table EMPLOYEE with the following fields - eno , first name , last name , dob , doj , dor , designation.

7. Write a statement to add a column manager to employess table with datatype as number

8. Write a query to prepare a table SHIFT with the following fields - shift code , shift start time , shift end time.

9. Write a query to prepare a table SWIPES with the following fields - eno , shiftcode , swipedate , swipe in time , swipe out time.

10. Write a query to add new fields Basic & Gender to the EMPLOYEE table.

11. Write a query to change the size of LAST_NAME field from 5 to 2.

12. Write a query to remove a DOR column from the table EMPLOYEE.

13. Write a query to create a back up table SWIPES in order to store only the the records of the year 2013.

Click here to download the solutions


Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/practice-problems-10

28 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

4.Data Manipulation Language


Objective
Introduction
INSERT statement
UPDATE statement
DELETE statement
4.1

Data Manipulation Language(DML)

4.2

Practice problems

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-manipulation-languagedml-0

29 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

4.1. Data Manipulation Language(DML)


Introduction
1. Data Manipulation Language(DML) is a structured query language which is used for inserting, updating and deleting data in the database
objects like table or view.
2. DML comprises the SQL change statements which modifies the stored data but not the database object.
3. DML consist of three SQL statements namelyInsert
Update
Delete

INSERT statement
Insert statement is used for inserting data into table.
Insertion of data can be done in multiple ways.
Syntax:
INSERT INTO table_name[(column1, column2,...)]
VALUES(value1, value2,....);
Ex:

If values in all the columns inserted in proper order, column names are not mandatory.
Syntax-:INSERT INTO table_name VALUES(value1, value2,....);
https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-manipulation-languagedml-1

30 / 56

Campus Commune

8/19/2014

Ex:

Insertion can also be done in interactive way.


The records can be inserted in interactive manner also
Syntax:
INSERT INTO table_name VALUES(&value1, &value2, ..);
Ex:

UPDATE statement
Update command is used to change or modify data of one or more records in a table.
Syntax:
UPDATE Table_name SET Column_name1=value1 [,Column_name2=value2,...]
[WHERE Condition];
Ex.

DELETE statement
1. Delete statement is used to remove one or more records from a table.
2. A subset may be defined for deletion using a condition, otherwise all records are removed.
Syntax:
DELETE FROM Table_Name
[WHERE Condition];
Delete statement using WHERE condition.
Ex:

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-manipulation-languagedml-1

31 / 56

Campus Commune

8/19/2014

Note: Delete statement without where condition deletes all the rows from table.
Example-

Related Videos

Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/data-manipulation-languagedml-1

32 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

4.2. Practice problems


Note
Q1 Q4 : Use the table PRODUCT prod_code,prod_name,price,manufacturer
Q5 : Use the table BOX - box_code,contents,value,warehouse
Q6 Q7 : Use the table EMPLOYEEempid,firstname,lastname,salary,department

1. Write a query to insert the above data into products table.

2. Write a query to take the values at run time and insert into products table.

3. Write statement to delete the price from products table for the code 6 and 7.

4. Write a statement to delete the products belongs to manufacturer 2,6.

5. Write a query to input the below information into the employee table.
Eno

First name

Last name

DOB

DOJ

DOR

NONE

10/06/83

10/06/03

NTWO

10/02/84

10/02/04

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/practice-problems-11

Designation

33 / 56

Campus Commune

8/19/2014

NTHREE

10/07/83

10/07/03

6. Write a query to input the below information into the BASIC field & GENDER field of the EMPLOYEE table.
Eno

Basic

Gender

10000

Male

15000

Female

10000

Female

Click here to download the solutions


Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/practice-problems-11

34 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

5.Transaction Control Language


Objective
Introduction to Transaction Control
Transaction Control Statements
5.1

Transaction Control Language (TCL)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/transaction-control-language

35 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

5.1. Transaction Control Language (TCL)


Introduction to Transaction
Oracle server ensures data consistency based upon transactions.
Transactions consist of DML statements that make up one consistent change to the data.

Transaction Start and End Scenarios:


A Transaction begins when the first executable sql statement is encountered.
The Transaction terminates when the following specifications occur.
A COMMIT or ROLL BACK issued.
A DDL statement issued
A DCL statement issued
Failure of machine or system crashes
A DDL or DCL statement is automatically committed and hence implicitly ends a Transaction.

Explicit Transaction Control Statements


COMMIT
SAVEPOINT
ROLLBACK
COMMIT:
It ends the current Transaction by making all pending data changes permanent.
Syntax:COMMIT;
Once commit is issued, data changes will become permanent.
https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/transaction-control-language-tcl-1

36 / 56

Campus Commune

8/19/2014

The previous state of the data is permanently lost.


All users can view the results of the recent transactional changes.
Ex: UPDATE employee SET salary =1000 WHERE emp_id = 10;
COMMIT;
SAVEPOINT:
It marks a savepoint with in the current Transaction.
We can create multiple savepoints in single Transaction.
Savepoints can be used to control the reverting of changes.
Syntax:SAVEPOINT <NAME>;
Ex: SAVEPOINT S1;
ROLLBACK:
It ends the current Transaction by discarding all pending data changes.
The data changes are undone.
The previous state of data is restored.
The locks on the affected rows are automatically released.
Syntax: ROLLBACK or ROLLBACK to <SAVEPOINT NAME>;
Ex: UPDATE employee SET salary =1000 WHERE emp_id = 10;
ROLLBACK;
using savepoint the Transaction can be discarded up to the marker by using rollback statement.
Ex: INSERT INTO employee VALUES(10,'JHON',3000);
INSERT INTO employee VALUES(10,'KELLY',2000);
SAVEPOINT S1;
INSERT INTO employee VALUES(10,'WILSON',4000);
ROLLBACK TO S1;

Related Videos

Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/transaction-control-language-tcl-1

37 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

6.Single Row, Aggregate / Group functions in SQL


Objective
Introduction to Oracle Built-in Functions
Single Row Functions and types
Conversion Functions
Character Functions
Numeric Functions
Dual table
ABS and MOD function
POWER and SQRT function
FLOOR and CEIL function
TRUNC and ROUND function
Numeric functions examples
Group functions
Using GROUP BY statement
Using WHERE clause
Using HAVING clause

6.1

Introduction to Oracle Built-in Functions

6.2

Single Row Functions

6.3

Conversion Functions

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/single-row-aggregate-group-functions-in-sql-0

38 / 56

Campus Commune

8/19/2014

6.4

Character Functions

6.5

Numeric Functions & Dual table

6.6

Aggregate/Group functions

6.7

Practice problems

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/single-row-aggregate-group-functions-in-sql-0

39 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

6.1. Introduction to Oracle Built-in Functions


Oracle Built-in Functions are the functions supplied by Oracle that can be used to manipulate data items and return a result.
There are two types of Built-in functions available in Oracle.
Single Row Functions: Single row or Scalar functions return a value for every row that is processed.
Group Functions: These functions group the rows of data based on the values returned by the query, performs the aggregate function(sum,avg
etc.) on each group and return one value per group of rows.
Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/introduction-to-oracle-built-in-functions-1

40 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

6.2. Single Row Functions


Single row or Scalar functions return a value for every row that is processed .
There are four different types of single row functions1)Conversion Functions: These are functions that help us to convert a value in one form to another form. For Example: a null value into an
actual value, or a value from one data type to another data type like NVL,TO_CHAR, TO_NUMBER, TO_DATE etc...
2)Character or Text Functions: These are functions that accept character input and can return both character and number values.
3)Numeric Functions: These are functions that accept numeric input and return numeric values.
4)Date Functions: These are functions that take values that are of data type DATE as input and return values of data type DATE, except for the
MONTHS_BETWEEN function, which returns a number.
Consider the following table Employee:

Related Videos

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/single-row-functions-1

41 / 56

Campus Commune

Ask a doubt

8/19/2014

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/single-row-functions-1

42 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

6.3. Conversion Functions


NVL
NVL2
NULLIF
NVL: let us replace null with a string in the results of a query .
Syntax : NVL( string1, replace_with).
If string1 is null, then NVL returns replace_with .
If string1 is not null, then NVL returns string1.
Ex: SELECT NVL(emp_name,'NA') FROM Employee;
The above query will display 'NA' wherever emp_name is null.
We can also replace with another column.
Ex: SELECT NVL(emp_name,dep_name) FROM Employee;
The emp_name and dep_name should belongs to same data type family.
The above query will display dep_name wherever emp_name is NULL.
Ex: SELECT NVL(salary,0) FROM Employee;
The above query returns 0 only if the salary is defined as NUMBER and is NULL.
NVL2: NVL2 function extends the functionality found in the NVL Function.
It lets you substitutes a value when a null value is encountered as well as when a non-null value is encountered.
Syntax : NVL2(string1,value_if_not_null,value_if_null)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/conversion-functions-0

43 / 56

Campus Commune

8/19/2014

if string1 is not null then NVL2 returns value_if_not_null.


if string1 is null then NVL2 returns value_if_null.
Ex: SELECT NVL2(emp_name,dep_name,'NOT AVAILABLE') FROM Employee;
NULLIF: NULLIF compares expr1 and expr2. If they are equal, then the function returns null. If they are not equal, then the function returns
expr1 .
Syntax : NULLIF(expr1,expr2)
Ex: SELECT NULLIF( dep_name,'HR') FROM Employee;
The above query returns NULL if dep_name field consists of 'HR', otherwise it returns job.
You cannot specify the literal NULL for expr1.
Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/conversion-functions-0

44 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

6.4. Character Functions


There are two types of character functions.
Character to character functions accept string as input and will give string as output.
INITCAP
LOWER
UPPER
CONCAT
LPAD,RPAD
TRIM
SUBSTR
REPLACE
Character to number functions accept string as input and will give number as output.
LENGTH
INSTR
INITCAP: This function sets the first character in each word to upper case and the rest to lower case.
Syntax : INITCAP(expr1)
Ex: SELECT INITCAP(emp_name) FROM Employee:
Amit
Ajay
Sima
Dipa
Anuj

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/character-functions-0

45 / 56

Campus Commune

8/19/2014

The above query returns all the employee names with the first letter in upper case and rest other characters in lower case.
LOWER: This function converts all letters in the specified string to lower case. If there are characters in the string that are not letters, they are
unaffected by this function.
Syntax : LOWER(expr1)
Ex: SELECT LOWER (emp_name) FROM employee:
amit
ajay
sima
dipa
anuj
The above query returns all the characters of the employee name in lower case.

UPPER:This function converts all letters in the specified string to uppercase. If there are characters in the string that are not letters, they are
unaffected by this function.
Syntax : UPPER(expr1)
Ex: SELECT UPPER (emp_name) FROM Employee:
AMIT
AJAY
SIMA
DIPA
ANUJ
The above query returns all the characters of the employee name in upper case.
CONCAT: This function allows you to concatenate two strings together.
Syntax : CONCAT(expr1,expr2)
Ex: SELECT CONCAT(emp_name,dep_name) full_name FROM Employee;
The above query returns the emp_name & dep_name concatenated into a single string.
SUBSTR: Returns specified characters from a string, starting from specific position to required characters length.
Syntax : SUBSTR(col/expr,m,n)
If 'm' is positive,oracle counts from beginning of string, If it is negative Oracle counts from the end of string.
If 'n' is omitted, Oracle returns all characters to end of the string starting from m.
Ex: SELECT SUBSTR(emp_name,3,2) FROM Employee.
The above query starts searching from the third position of the employee name from the starting of the string and displays two characters from
there.
Ex: SELECT SUBSTR('abcdefg'-4,2) FROM dual;
dc
The above query starts searching from the fourth position of the given string from the end of the string and display two characters from there.
REPLACE: It returns the every occurrence of search_string replaced by the replacement_string.
If the replacement string is omitted or null all occurrences of search string will be removed.
Syntax : REPLACE(string,search_string,replace_string)
Ex: SELECT REPLACE(branch_name,'Mumbai','Kolkata') FROM Employee WHERE dep_name = 'HR';
The above query replaces branch name to 'Kolkata' wherever 'Mumbai' is available for the HR department .

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/character-functions-0

46 / 56

Campus Commune

8/19/2014

LPAD,RPAD:
LPAD pads the character value right justified to a total width of n character positions.
Syntax : LPAD(expr1,padded_length,padded_string)
RPAD pads the character value left justified to a total width of n character positions.
Syntax : RPAD(expr1,padded_length,padded_string)
The default padding character is space.
Ex: SELECT LPAD('jhon',8) FROM dual;
jhon
Since the third parameter is not specified in the result of the above query will be by default space padded for the previous four positions.
Ex2: SELECT LPAD('jhon',8 ,'x') FROM dual;
xxxxjhon
The above query fills the four blank spaces with 'x' left of the given string.
Ex3: SELECT RPAD('jhon',8 ,'x') FROM dual;
jhonxxxx
The above query fills the four blank spaces with 'x' right of the given string.
TRIM: It enables to trim leading or trailing characters or both from a string.
If we dont specify anything, it will trim spaces.
Syntax : TRIM( [LEADING | TRAILING | BOTH] character FROM string)
LTRIM : Removes the leading characters
RTRIM : Removes the trailing characters
TRIM : Removes both
Ex: SELECT RTRIM('s' from 'ssmithss') FROM dual;
ssmith
The above query removes 'trailing' 's' from the given string.
Ex: SELECT LTRIM('s' from 'ssmithss') FROM dual;
mithss
The above query removes 'leading' 's' from the given string.
Ex: SELECT TRIM('s' from 'ssmiths') FROM dual;
mith
The above query removes 'trailing' & 'leading' 's' from the given string.
Ex: SELECT TRIM(' smith ') FROM dual;
Smith
The above query removes 'trailing' & 'leading' spaces from the given string.
INSTR: This function returns the location of a sub string in a given string.
Syntax : INSTR( string, sub_string [, start_position [, nth_appearance ] ] ).
start_position and nth_appearance are optional. If not specified, always INSTR starts with first position and will give first appearance.
Ex: SELECT INSTR('internet','e') FROM dual;
4
The above query returns the first position of 'e' searched from the start of the given string.
Ex: SELECT INSTR('internet','e',1,2) FROM dual;

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/character-functions-0

47 / 56

Campus Commune

8/19/2014

7
The above query returns the second position of 'e' searched from the start of the given string.
Ex: SELECT INSTR('internet','e',5,1) FROM dual;
3
The above query returns the first position of 'e' searched from the fifth position of the given string.
LENGTH: Returns number of characters in a value.
Syntax : LENGTH(column)
Ex: SELECT LENGTH(branch_name) FROM Employee;
The above query returns number characters in the branch_name field for each and every record.
Ex: SELECT LENGTH('jhon') FROM dual;
4
The above query returns the number of characters from the given string.
Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/character-functions-0

48 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

6.5. Numeric Functions & Dual table


1. Numeric functions are used to perform operations on numbers.
2. They accept numeric values as input and return numeric values as output.
3. Following are the few examples of Numeric functions available in Oracle.
a.
b.
c.
d.

ABS and MOD


POW and SQRT
FLOOR and CEIL
TRUNC and ROUND

Dual table
1. This is a single row and single column dummy table provided by oracle. This is used to perform mathematical calculations without using a
table.
2. Oracle presents the output of every operations in a tabular format so that it seems to the user that the output comes from a table

ABS and MOD Function


The ABS function returns the absolute value of the parameter passed.
Syntax : ABS(number)
Ex: SELECT ABS(-10) FROM dual;
10

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/numeric-functions-1

49 / 56

Campus Commune

8/19/2014

The above query returns the absolute value of the given 'number'.
The MOD function returns the remainder value of the parameter passed.
Syntax : MOD(number1,number2)
Ex: SELECT MOD(10,4) FROM dual;
2
The above query returns the remainder when 10 is divided by 4.

POWER and SQRT Function


POWER function returns the argument raised to the specified power.
Syntax : POWER(number1,number2)
Ex: SELECT POWER(4,3) As Cube FROM dual;
64
The above query returns the output when 4 is raised to the power of 3.
SQRT function returns the square root of a number passed as parameter.
Syntax: SQRT(number)
Ex: SELECT SQRT(64) As Square Root FROM dual;
8
The above query returns the square root value of 64.

FLOOR and CEIL Function


The FLOOR function returns the largest integer less than or equal to the value
passed in parameter.
Syntax: FLOOR(decimal number)
Ex: SELECT FLOOR(7.14), FLOOR(7.84) FROM dual;
7

The above query returns the largest integer nearest to 7.14 & 7.84.
Ex: SELECT FLOOR(-7.14) FROM dual;
-8
The above query returns the largest integer nearest to -7.14.
The CEIL function returns the smallest integer greater than or equal to the value
mentioned in parameter.
Syntax: CEIL(decimal number)
Ex: SELECT CEIL(7.14), CEIL(7.84) FROM dual;
8

The above query returns the smallest integer nearest to 7.14 & 7.84.
Ex: SELECT CEIL(-7.14) FROM dual;
-7
The above query returns the smallest integer nearest to -7.14.

TRUNC and ROUND Function

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/numeric-functions-1

50 / 56

Campus Commune

8/19/2014

1) The TRUNC function truncates the value present in the column, expression up to decimal places mentioned in first parameter.
2) If the second argument is 0 or is missing, the value is truncated to zero decimal places.
Syntax: TRUNC(decimal number,number of places)
Ex: SELECT TRUNC(137.5738,3) As Rounded FROM dual;
137.573
The above query returns the decimal number with three digits after the decimal point.
Ex: SELECT TRUNC(137.5738,0) As Rounded FROM dual;
137
The above query returns the integer value.
The ROUND function round off the value present in the column, expression up to decimal places mentioned in first parameter.
If the second argument is 0 or is missing, the value is rounded to zero

decimal places.

Syntax: ROUND(decimal number,number of places)


Ex: SELECT ROUND(137.5738,3) As Rounded FROM dual;
137.574
The above query returns the decimal number with three digits after the decimal point where 4th digit is rounded.
SQL> SELECT ROUND(137.5738,0) As Rounded FROM dual;
138
The above query returns the integer value rounded to the next highest value.
If the second argument is negative number, the value is rounded up specified decimal places to the left (rounded to the nearest unit of 10).
Ex: SELECT ROUND(137.5748,-1) As Rounded FROM dual;
140
The above query returns the integer value which is the nearest tens value.
Ex: SELECT ROUND(137.5748,-2) As Rounded FROM DUAL;
100
The above query returns the integer value which is the nearest hundreds value.
Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/numeric-functions-1

51 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

6.6. Aggregate/Group functions


Aggregate / Group Functions
Group Functions, as the name suggests, are functions that operate on groups(sets) of values and returns one result per group.
Group function returns a single result row for every group of queried rows.
Based on the query statement it may return single or multiple rows.
The functions that are used as Aggregate Functions are:
SUM
AVG
MAX
MIN
COUNT
Consider the following table: Employee

AVG: Returns an average value, ignoring null values.


Syntax: AVG([DISTINCT] column_name)
Ex: SELECT AVG(salary) as Average Salary FROM Employee;
21500

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/this-is-a-single-row-and-single-column-dummy-table

52 / 56

Campus Commune

8/19/2014

The above query displays the average salary of all the employees in the table Employee
MAX: Returns the maximum value, ignoring null values.
Syntax: MAX([DISTINCT] column_name)
Ex: SELECT MAX(salary) as Maximum Salary FROM Employee where Dep_Name='HR';
16000
The above query displays the maximum salary of all the employees in HR Department in the table Employee.
MIN: Returns the minimum value, ignoring null values.
Syntax: MIN([DISTINCT] column_name)
Ex: SELECT MIN(salary) as Minimum Salary FROM Employee where Dep_Name='HR';
10000
The above query displays the minimum salary of all the employees in HR Department in the table Employee.
COUNT: Returns the count of not null values ignoring null values.
Syntax: COUNT([DISTINCT] column_name)
Ex: SELECT COUNT(DISTINCT Dep_name) Departments FROM Employee;
3
The above query displays the count of different departments in the table Employee.
COUNT:(*) Count function with asterisk returns the count of total number of rows including null values
Syntax: COUNT(*)
Ex: SELECT COUNT(*) FROM Employee;
5
The above query displays the total number of rows in table Employee.

GROUP BY clause
Creates a data set, containing several sets of records grouped together based on a condition.
Syntax: SELECT <columnName1>[,<columnName2>], AGGREGATE FUNCTION(<expression>) FROM Table_Name GROUP BY
<columnName1>[,<columName2>] ;
Ex: SELECT dep_name,COUNT(emp_id) "No of Employee" FROM Employee GROUP BY dep_name;
HR

Marketing

Admin

The above query displays the number of employee in each department.

WHERE clause
Used to apply a filter condition before the Grouping the rows.
Syntax: SELECT <columnName1>[,<columnName2>], AGGREGATE FUNCTION(<expression>) FROM Table_Name WHERE
<condition_before_grouping_rows> GROUP BY <columnName1>[,<columName2>] ;
Ex: SELECT Dep_Name,COUNT(Salary) FROM Employee WHERE Salary>15000 GROUP BY Dep_Name;
HR

Marketing

Admin

The above query displays department wise count of salary more than 15000.

HAVING clause
Used to apply a filter condition on Aggregate values.

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/this-is-a-single-row-and-single-column-dummy-table

53 / 56

Campus Commune

8/19/2014

Syntax: SELECT <columnName1>[,<columnName2>], AGGREGATE FUNCTION(<expression>) FROM Table_Name WHERE


<condition_before_grouping_rows> GROUP BY <columnName1>[,<columName2>] HAVING <condition_on_grouped_result>;
Ex: SELECT Dep_Name, SUM(Salary) FROM Employee WHERE Salary>12000
GROUP BY Dep_Name HAVING SUM(Salary)<30000;
HR

16000

Marketing

20000

The above query displays the departments for which total salary is less 30000 excluding the Admin department, total salary for which is 40000.

Related Videos

Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/this-is-a-single-row-and-single-column-dummy-table

54 / 56

Campus Commune

8/19/2014

Pre-ILP Home
Unix/C++/Oracle Lounge
Logout

Oracle
Q
1.Database Concepts
Q
2.Data Query Language
Q
3.Data Definition Language
Q
4.Data Manipulation Language
Q
5.Transaction Control Language
Q
6.Single Row, Aggregate / Group functions in SQL

Course Completion Quiz

6.7. Practice problems


Topic: Aggregate Functions
Note
Q1 Q3 : Use the table BOX - box_code,contents,value,warehouse
Q4 Q8 : Use the table EMPLOYEEempid,firstname,lastname,salary,department
Q9 Q11 : Use the table SWIPES - empid , shiftcode , swipedate , swipe in time , swipe out time.

1. Write a statement to get the count of boxes for each warehouse.


2. Write a statement to get the count of different contents in box table.
3. Write a statement to get the warehouse which is containing contents equal to 100.
4. Write a query to get sum,average,min salary for each department.
5. Write a statement to get the departments containing more than one employee.
6. Write a query to display the total number of employees available in the organization.
7. Write a query to display the maximum salary given to the employees in the organization.
8. Write a query to display the maximum salary paid for the employees belonging to each designation. Display only those records whose
maximum salary is more than 10000.
9. Write a query to display the number of different swipe dates available in the SWIPES table.
10. Write a query to display the total number of swipe records available for each shift.
11. Write a query to display the total number of swipe records available for each shift for the swipedate '21-dec-13'.
https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/practice-problems-12

55 / 56

Campus Commune

8/19/2014

Click here to download the solutions

Topic : Numeric Functions


Note:
Q1 : Use the table EMPLOYEEempid,firstname,lastname,salary,department
1. Write a query to to get the employeename,salary and bonus
calculate bonus based on the following formula.
Bonus = salary *0.10.. Round to nearest integer
2. Write a query to print the given value as 121 , and root value of 121
3. Write a query to get the 6 th power of 5.
4. Write a statement to get the next least integer for -1.67
5. Write a statement to get the next highest integer for 10.234
6. Write a statement to get reminder of 2543/285 rounded to 2 decimals
7. Write a statement to print the nearest tens value which is smaller than the given input 108.59.
8. Write a statement to print the nearest tens value which is bigger than the given input 108.59.

Click here to download the solutions


Ask a doubt

(Misuse of 'Ask a Doubt' Section will be dealt as per the Terms & Conditions of Campus Commune)

Open Doubts

Closed Doubts

PADMINI T
Is there any option to reset tech lounge quizzes?
about 2 months ago

APURVA MISHRA
no

https://campuscommune.tcs.com/communities/unix-c-oracle-lounge/content/practice-problems-12

56 / 56

You might also like