0% found this document useful (0 votes)
5 views12 pages

7-Queries With Aggregate Functions (Group by and Having Clause) - 19!02!2024

The document provides a comprehensive overview of SQL (Structured Query Language), detailing its commands for creating and managing databases, including table creation, data entry, and constraints. It explains key concepts such as relational databases, primary and foreign keys, and various SQL commands for data manipulation and retrieval. Additionally, it includes examples of SQL queries and the use of aggregate functions, as well as guidelines for defining constraints and executing commands from a .sql file.

Uploaded by

itsrajsharma29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

7-Queries With Aggregate Functions (Group by and Having Clause) - 19!02!2024

The document provides a comprehensive overview of SQL (Structured Query Language), detailing its commands for creating and managing databases, including table creation, data entry, and constraints. It explains key concepts such as relational databases, primary and foreign keys, and various SQL commands for data manipulation and retrieval. Additionally, it includes examples of SQL queries and the use of aggregate functions, as well as guidelines for defining constraints and executing commands from a .sql file.

Uploaded by

itsrajsharma29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

type sqlplus at command prompt

alternatively type sqlplus in search text box

username: 22BIT0025@SITEORA

password: 22BIT0025

To change your password type

SQL>password

SQL (Structured Query Language) -- This is not a programming language

Command based language

SQL is a comprehensive database language to create and maintain a database.

A database is a collection of related data with implicit meaning.

Relational database

A relational database is a collection of inter-related tables.

A table is formally called a relation.

SQL is a command based language:

create

alter command
insert

update

delete

select (data retrieval. read command)

Metadata means data (schema data) about data (user data)

The description of a database is called schema of the database.

Data present in a database at a particular instance of time is called database instance.

SQL is not a case sensitive language.

create table Employee (SSN char(9) primary key,

fname varchar(20) not null,

Minit char(1),

Lname varchar(20) not null,

Sex char(1),

Address varchar(100),

salary number(10),

DOB date,

Department number(1),

designation varchar(20),

SupervisorSSN char(9)

);
create table Department (Dnumber number(1) primary key,

Name varchar(30) not null unique ,

ManagerSSN char(9),

Manager_DOB date,

Location varchar(60)

);

unique means duplicate values are not permitted.

not null means value is mandatory.

SQL> select * from tab;

you can view all tables from the database

tab is system table.

SQL>drop table table_name; -- removes a table

A table can have at the most one primary key; but it can have multiple secondary keys.

Use the keyword unique to define a secondary key.

create table Dependent (Name varchar(50),

DOB date,

Sex char,

Relationship varchar(30),
EmployeeSSN char(9),

primary key(name, employeessn)

);

create table Project (Pnumber number(2) primary key,

Name varchar(20),

Location varchar(40),

ControllingDepartment number(1),

Budget number(20)

);

create table Works_on (SSN char(9),

ProjectNum number(2),

Hours number(4, 2),

primary key(ssn, projectnum)

);

Data entry (interactive data entry)

***********

insert into employee1 values (&SSN, &fname, &Minit, &Lname, &Sex, &Address, &salary, &DOB,
&Department, &designation, &SupervisorSSN);

To save the changes in user data; use

commit;

To undo the changes use


rollback;

But committed data cannot be rolled back.

To see the data that you have entered into the table use

select * from employee;

To execute commands using .sql file:

Create a file containing necessary SQL commands with extension .sql

Now at the SQL prompt

SQL>@file-path

Data entry

***********

insert into employee values (&SSN, &fname, &Minit, &Lname, &Sex, &Address, &salary, &DOB,
&Department, &designation, &SupervisorSSN);

Constraint definition

==================

ALTER TABLE table_name ADD CHECK (condition);

ALTER TABLE table_name ADD [CONSTRAINT constraint_name] CHECK (condition);


i. Department number should be in the range 1000 to 2000.

i. Department number should be in the range 1 to 9.

alter table department add check (dnumber between 1 and 9);

alter table department add [constriant dept_chk] check (dnumber between 1 and 9);

To find out constraint name:

select constriant_name, constraint_type from user_constraints where table_name = 'DEPARTMENT';

To drop a constriant use:

ALTER TABLE table_name DROP CONSTRAINT constraint_name;

Foreign key:

A foreign key establishes a relationship between two tables.

A foreign key is a column that appears as a key column in some other table.

The other table can be the same table.

A foreign key can be composite in nature.

The key column can be primary key / secondary key.

The domain of foreign key column must be the same as that of the referencing column (key column it
refers to).

The data type along with size (may be not null also) defines domain of a column.

ssn char(9) Here char(9) is the domain of ssn

fname varchar(20) Here varchar(20) is the domain of fname.


How to define a foreign key?

ALTER TABLE table_name ADD FOREIGN KEY (col1, col2,...) REFERENCES other_table_name(col1,
col2, ...);

alter table employee12 add foreign key (department) references department12(dnumber);

alter table employee12 add foreign key (supervisorssn) references employee12(ssn);

Foreign key constraint is also called Referential Integrity constraint.

Check constraint & Foreign key

Check constraint

===============

ii) Relationship of the dependents to an employee should be only Spouse, Son, Daughter, and Parent.

alter table dependent12 add check (relationship in ('Spouse', 'Son', 'Daughter', 'Parent') );

iii. Name of the project doesn’t exceed 4 characters.

iii. Name of the project doesn’t exceed 10 characters.

alter table project12 add check ( length(pname) <= 10);

****************************************************************************

February 19, 2024

d)
1. Display all the employee details of the employee table

select * from employee;

select <list of columns separated by comma> from table_name;

e.g. select ssn, fname, lname, salary from employee;

You may also use alias in select statement.

e.g. select ssn, fname "First name", lname, salary from employee;

Display first name, last name, ssn and salary for employees with

salary higher than 30000.

select fname, lname, ssn, salary from employee where salary > 30000;

select fname, lname, ssn, salary from employee

where salary between 30000 and 40000;

select fname, lname, ssn, salary from employee

where salary > 30000 and department = 4;

and, or, not

Display employees who do not belong to any of the departments.


select ssn, fname, lname from employee where department is null;

select <list of columns separated by comma>

from table_name

where <condition>;

Execution sequence:

/* Step 3 */select <list of columns separated by comma>

/* Step 1 */ from table_name

/* Step 2 */where <condition>;

Display name of the employees whose first name starts with an A.

select fname, lname

from employee

where fname like 'A%'

select fname, lname

from employee

where fname like 'A___';

select fname, lname

from employee
where fname like 'A%' and length(fname)=4;

% implies zero or more characters.

_ implies exactly one character.

alter student add check (email like '%@%') -- for defining check constraint

Display ssn, fname and lname for employees whose address has _ in it.

select fname, lname, ssn from employee

where address like '%\_%' escape '\';

select fname, lname, ssn from employee

where address like '%\%%' escape '\';

select name, dob, to_char(dob, 'Month dd, yyyy') "Birth Date"

from dependent;

select name, dob, to_char(dob, 'Day Month dd, yyyy') "Birth Date"

from dependent;

5. Dependent names that are females and related to Employee no 15678.

select name from dependent

where sex = 'F' and employeessn = '123456789';


Interactive query:

select name from dependent

where sex = &sex and employeessn = &employeessn;

7. Find the employee who does not have a supervisor.

select fname, lname from employee where supervisorssn is null;

16. Count the number of projects for any SSN.

select count(projectnum) from works_on12 where ssn = '&ssn';

Aggregate functions:

sum

count

avg

max

min

median

select avg(salary)*count(ssn), sum(salary) from employee12;

count(salary), count(ssn)

count(distinct salary)

select salary from employee;


select distinct salary from employee;

distinct clause removes duplicate from output.

select fname, lname, salary from employee;

select ssn, fname, lname, salary from employee;

select distinct fname, lname, salary from employee;

select ssn, fname, lname, salary from employee order by salary;

data will be displayed in ascending order of salary.

If you want to display data in descending order of salary then use:

select ssn, fname, lname, salary from employee order by salary desc;

select ssn, fname, lname, salary from employee order by fname;

select ssn, fname, lname, salary from employee order by fname, lname;

select ssn, fname, lname, salary from employee

order by fname, lname desc;

You might also like