0% found this document useful (0 votes)
10 views3 pages

SQL Assignment Day Practice

The document contains 4 problems involving SQL queries and database tables. Problem 1 creates a Faculty table and inserts records, then uses a CASE statement to return standardized department names. Problem 2 adds a calculation of vacation hours to the Faculty table. Problem 3 defines primary keys, candidate keys, prime attributes and non-prime attributes. Problem 4 creates tables for students and marks, inserts records, and uses a LEFT JOIN and CASE to return total marks or null for each student.

Uploaded by

PRIYOBRATO BANIK
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)
10 views3 pages

SQL Assignment Day Practice

The document contains 4 problems involving SQL queries and database tables. Problem 1 creates a Faculty table and inserts records, then uses a CASE statement to return standardized department names. Problem 2 adds a calculation of vacation hours to the Faculty table. Problem 3 defines primary keys, candidate keys, prime attributes and non-prime attributes. Problem 4 creates tables for students and marks, inserts records, and uses a LEFT JOIN and CASE to return total marks or null for each student.

Uploaded by

PRIYOBRATO BANIK
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/ 3

Problem 1;

===================================================================================
=========================================================================
create Table Faculty
(FacultyID int, Name varchar(18), Department varchar(5), Gender varchar(1), Salary
int, Working_hours int);

insert into Faculty values(001,'Aakash','CS','M',50000,45);

insert into Faculty values(001,'Sahil','ES','M',25000,50);


insert into Faculty values(002,'John','HSS','M',100000,60);
insert into Faculty values(003,'Shelley','CS','F',75000,80);
insert into Faculty values(004,'Anannya','CS','F',45000,100);
insert into Faculty values(005,'Sia','HSS','F',60000,40);

mysql> Select facultyid,name,Case


-> When faculty.department = "CS" then "Computer Science"
-> When faculty.department = "EC" then "Electonics and Communication"
-> When faculty.department = "HSS" then "Humanities and Social Sciences"
-> end as department, gender,salary,working_hours from faculty;

+-----------+---------+--------------------------------+--------+--------
+---------------+
| facultyid | name | department | gender | salary |
working_hours |
+-----------+---------+--------------------------------+--------+--------
+---------------+
| 1 | Aakash | Computer Science | M | 50000 |
45 |
| 1 | Sahil | NULL | M | 25000 |
50 |
| 2 | John | Humanities and Social Sciences | M | 100000 |
60 |
| 3 | Shelley | Computer Science | F | 75000 |
80 |
| 4 | Anannya | Computer Science | F | 45000 |
100 |
| 5 | Sia | Humanities and Social Sciences | F | 60000 |
40 |
+-----------+---------+--------------------------------+--------+--------
+---------------+

===================================================================================
========================================================================
Problem-2;
--------------

create Table Faculty


(FacultyID int, Name varchar(18), Department varchar(5), Gender varchar(1), Salary
int, Working_hours int);

insert into Faculty values(001,'Aakash','CS','M',50000,45);


insert into Faculty values(001,'Sahil','ES','M',25000,50);
insert into Faculty values(002,'John','HSS','M',100000,60);
insert into Faculty values(003,'Shelley','CS','F',75000,80);
insert into Faculty values(004,'Anannya','CS','F',45000,100);
insert into Faculty values(005,'Sia','HSS','F',60000,40);

mysql> select facultyid, name,case when working_hours <=50 then 'not allowed' when
working_hours <= 80 then 20 when working_hours <= 120 then 40 end as
vacationhours from faculty;

+-----------+---------+---------------+
| facultyid | name | vacationhours |
+-----------+---------+---------------+
| 1 | Aakash | not allowed |
| 1 | Sahil | not allowed |
| 2 | John | 20 |
| 3 | Shelley | 20 |
| 4 | Anannya | 40 |
| 5 | Sia | not allowed |
+-----------+---------+---------------+
6 rows in set (0.00 sec)

===================================================================================
====================================================================
problem -3:

Q3.Explain what is a PK, Candidate Key, Prime Attributes, Non-prime Attributes ?


----------------------------------------------------------------------------------

#Prime key (PK)?


----------------------
Ans- PRIMARY KEY in DBMS is a column or group of columns in a table that uniquely
identify every row in that table. The Primary Key can�t be a duplicate meaning
the same value can�t appear more than once in the table. A table cannot have more
than one primary key.

#Candidate key :
--------------------

Candidte key in SQL is a set of attributes that uniquely identify tuples in a


table. Candidate Key is a super key with no repeated attributes. The Primary key
should be selected from the candidate keys. Every table must have at least a single
candidate key. A table can have multiple candidate keys but only a single primary
key.

Properties of Candidate key:

It must contain unique values


Candidate key in SQL may have multiple attributes
Must not contain null values
It should contain minimum fields to ensure uniqueness
Uniquely identify each record in a table

#Prime Attributes :
Attributes of the database tables which are candidate keys of the database tables
are called prime attributes.

#Non-Prime attributes:
Attributes of the database tables which do not exist in any of the possible
candidate keys of the database tables are called non-prime attributes.

===================================================================================
====================================================================
Problem-4
------------------

create table
students(student_id int primary key,name varchar(12),address varchar(12));

create table marks(marks_id int primary key,subject varchar(12),score


int,student_id int, foreign key marks(student_id) references students(student_id));

insert into students values(1,"Ritesh","Bangalore");


insert into students values(2,"Shubham","Mumbai");
insert into students values(3,"Rohit","Delhi");

insert into marks values(1,"sst",100,1);


insert into marks values(2,"science",100,1);
insert into marks values(3,"sst",99,2);
insert into marks values(4,"science",80,2);

mysql> select students.student_id,students.name,case


-> when sum(score) is null then -1 else sum(score)
-> end as total_marks from students left join marks on students.student_id
= marks.student_id group by students.student_id;
+------------+---------+-------------+
| student_id | name | total_marks |
+------------+---------+-------------+
| 1 | Ritesh | 200 |
| 2 | Shubham | 170 |
| 3 | Rohit | -1 |
+------------+---------+-------------+
3 rows in set (0.01 sec)

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy