7-Queries With Aggregate Functions (Group by and Having Clause) - 19!02!2024
7-Queries With Aggregate Functions (Group by and Having Clause) - 19!02!2024
username: 22BIT0025@SITEORA
password: 22BIT0025
SQL>password
Relational database
create
alter command
insert
update
delete
Minit char(1),
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,
ManagerSSN char(9),
Manager_DOB date,
Location varchar(60)
);
A table can have at the most one primary key; but it can have multiple secondary keys.
DOB date,
Sex char,
Relationship varchar(30),
EmployeeSSN char(9),
);
Name varchar(20),
Location varchar(40),
ControllingDepartment number(1),
Budget number(20)
);
ProjectNum number(2),
);
***********
insert into employee1 values (&SSN, &fname, &Minit, &Lname, &Sex, &Address, &salary, &DOB,
&Department, &designation, &SupervisorSSN);
commit;
To see the data that you have entered into the table use
SQL>@file-path
Data entry
***********
insert into employee values (&SSN, &fname, &Minit, &Lname, &Sex, &Address, &salary, &DOB,
&Department, &designation, &SupervisorSSN);
Constraint definition
==================
alter table department add [constriant dept_chk] check (dnumber between 1 and 9);
Foreign key:
A foreign key is a column that appears as a key column in some other table.
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.
ALTER TABLE table_name ADD FOREIGN KEY (col1, col2,...) REFERENCES other_table_name(col1,
col2, ...);
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') );
****************************************************************************
d)
1. Display all the employee details of the employee table
e.g. select ssn, fname "First name", lname, salary from employee;
Display first name, last name, ssn and salary for employees with
select fname, lname, ssn, salary from employee where salary > 30000;
from table_name
where <condition>;
Execution sequence:
from employee
from employee
from employee
where fname like 'A%' and length(fname)=4;
alter student add check (email like '%@%') -- for defining check constraint
Display ssn, fname and lname for employees whose address has _ in it.
from dependent;
select name, dob, to_char(dob, 'Day Month dd, yyyy') "Birth Date"
from dependent;
Aggregate functions:
sum
count
avg
max
min
median
count(salary), count(ssn)
count(distinct salary)
select ssn, fname, lname, salary from employee order by salary desc;
select ssn, fname, lname, salary from employee order by fname, lname;