0% found this document useful (0 votes)
6 views4 pages

SQL Practise

The document provides an overview of SQL queries related to employee and student data, including salary totals by department, average scores by subject, and employee counts per department. It also discusses database constraints such as primary keys, foreign keys, and checks to maintain data integrity. Additionally, it highlights the importance of joins in managing data across multiple tables.

Uploaded by

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

SQL Practise

The document provides an overview of SQL queries related to employee and student data, including salary totals by department, average scores by subject, and employee counts per department. It also discusses database constraints such as primary keys, foreign keys, and checks to maintain data integrity. Additionally, it highlights the importance of joins in managing data across multiple tables.

Uploaded by

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

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

Display the total salary for each job with in the department

emp
====
eid ename did salary
jid
--------------------------------------------------------------------------------
100 a 10 10000 j1
101 b 10 15000
j2
102 c 20 90000 j1
103 d 30 10000
j3
104 e 30 20000 j1
105 f 25 10000
j2
106 g - 10000
j1

did jid sum(salary)

E1,E2,E3 -->three batches


select avg(score) from emp group by subject;

linux 9
windows 8
oracle 8
sql 9
tsql 9

Batchname scholar name subject score


=============================================
E1 s1 linux 8
E1 s2 linux 7
E1 s3 linux 8
E1 s1 Windows
8
E1 s2 Windows
7
E1 s3 Windows
8
E2 s11 linux 8
E2 s12 linux 7
E2 s13 linux 8
E2 s11 Windows
8
E2 s12 Windows
7
E2 s13 Windows
8

What is the average score of linux module?


select avg(score) from stu where subject='Linux';
What is the average score of each module?
select avg(score) from stu group by subject;
Display Batchwise average score of each subject
select batchname,subject,avg(score) from stu group by batchname,subject
batchname subject avg(acore)
-------------------------------------------------
E1 linux 7.6
E1 Windows 7.6
E2 linux 7.6
E2 Windows 7.6

Emp
====
eid ename did salary
jid
--------------------------------------------------------------------------------
100 a 10 10000 j1
101 b 10 15000
j2
102 c 20 90000 j1
103 d 30 10000
j3
104 e 30 20000 j1
105 f 25 10000
j2
106 g - 10000
j1

display the total no of employees working in each department


select count(eid) from emp group by did;

did count(did)
10 2
20 1
30 2
25 1

Display the department where minimum no of employees(1) are working.


select did,count(did)
from emp
group by did
having count(*) =1;

where-->filter the records


having-->filter the groups

Display the dept details contains 5 employees


Emp
====
eid ename did salary
jid
--------------------------------------------------------------------------------
100 a 10 10000 j1
101 b 10 15000
j2
102 c 20 90000 j1
103 d 30 10000
j3
104 e 30 20000 j1
105 f 25 10000
j2
106 g - 10000
j1

select did,count(*) from emp group by did having count(*)>1;

did count(did)
10 2
20 1
30 2
25 1

select count(*) from emp group by did having count(*)=5;

count(*)
2
1
2
1

select jid,count(*) from emp group by did, having count(*)=5;


-->error
-->columns present in the group by clause may or may not present in the select
clause
--->coulmn present in the select clause(except hroup function) must be present in
group by clause
================================================================
Constraints
=========
To remove the duplicates
To maintain the referential integrity

Primary key----------->unique and not null


Foreign Key------------>Child key always refers primary key values
Check------------------->check some condition before insert /update
Unique ----------------->avoid duplicate values
Not null----------------->Value can't be empty

create table emp(eid int primary key,name varchar(40) not null,salary int
check(salary>0),mnumber int unique);

dept
====
(PK)
did dname loc
---------------------------
10 HR Bang
20 IT VJW
30 ACC HYD
40 FIN CHN

EMP
===
eid ename age did salary
====================================
100 a 20 50 10000------------->error
record
100 a 20 10 10000--------------
>valid record
101 b 20 10 65779------------->valid record
while entering the records(did column) into the emp table, dept table's did column
must be verified.It can be achieved through the foreign key concept

foreign key allows duplicates and null values also.

Table can have only one primary key.but primary key column may contain more than
one column.
Table level constarints
===================
create table stud (regno int,rollno int,name varchar(30),age int,constraint c_name
primary key(regno,rollno),check (age>0));

Column level constraints


========================
create table dept(did int primary key,dname varchar(90),loc varchar(30));
create table emp(eid int primery key,ename varchar(30),age int,did int references
dept(did),salary int);

*************NOt null constraints allowed only in column level**********

Joins
====
Data will be stored in tables
data will be stored across multiple tables.(normalization)

You might also like