0% found this document useful (0 votes)
25 views

SQL Assignment Day6

The document contains SQL queries that create tables for contacts, professions, zip codes, and statuses, and inserts data into those tables. It then performs inner joins between the contacts table and each of the other tables to select columns and return matching records based on primary and foreign keys.

Uploaded by

noadviceno
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

SQL Assignment Day6

The document contains SQL queries that create tables for contacts, professions, zip codes, and statuses, and inserts data into those tables. It then performs inner joins between the contacts table and each of the other tables to select columns and return matching records based on primary and foreign keys.

Uploaded by

noadviceno
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL ASSIGNMENT DAY 6

Qureies:

create table PROFESSION(Prof_Id int PRIMARY KEY NOT NULL,Profession varchar(20));

create table ZIP_CODE(Zip_Code varchar(20) PRIMARY KEY, City varchar(20), State


varchar(20));

create table [STATUS] (Status_id int PRIMARY KEY, Status varchar(20));

create table MY_CONTACTS(Contact_Id int PRIMARY KEY NOT NULL, Last_Name varchar(20),
First_Name varchar(20), Phone varchar(20), Email varchar(20), Gender varchar(20),
Birthday varchar(20), Prof_Id int FOREIGN KEY REFERENCES PROFESSION(Prof_Id), Zip_Code
varchar(20) FOREIGN KEY REFERENCES ZIP_CODE(Zip_Code), Status_Id int FOREIGN KEY
REFERENCES STATUS(Status_Id));

ALTER TABLE MY_CONTACTS ALTER COLUMN Birthday DATE;

insert into PROFESSION values(1,'Software Engineer')


insert into PROFESSION values(2,'HR Manager')
insert into PROFESSION values(3,'CEO')
insert into PROFESSION values(4,'Accounts Manager')
insert into PROFESSION values(5,'Admin')

insert into ZIP_CODE values('635610','Tirupattur','TN')


insert into ZIP_CODE values('653471','Vellore','TN')
insert into ZIP_CODE values('673861','Bangalore','KA')
insert into ZIP_CODE values('634513','Selam','TN')
insert into ZIP_CODE values('634510','Mysore','KR')

insert into [STATUS] values(1,'Yes')


insert into [STATUS] values(2,'Yes')
insert into [STATUS] values(3,'Yes')
insert into [STATUS] values(4,'Yes')
insert into [STATUS] values(5,'No')

insert into MY_CONTACTS values(1,'baran','guru', '7010976093', '[email protected]', 'Male',


'2000/07/17',3,'673861',1)
insert into MY_CONTACTS values(2,'karan','hari', '7010976094', '[email protected]', 'Male',
'2000/07/18',1,'634513',2)
insert into MY_CONTACTS values(3,'waran','vignesh', '7010976095', '[email protected]',
'Male', '2000/07/19',2,'634510',3)
insert into MY_CONTACTS values(4,'kandan','mani', '7010976096', '[email protected]', 'Male',
'2000/07/20',5,'635610',5)
insert into MY_CONTACTS values(5,'kumar','arun', '7010976097', '[email protected]', 'Male',
'2000/07/21',4,'653471',4)
select a.Last_Name, -- to fetch column called lastname from the mycontacts table
named as a

a.First_Name, -- to fetch column called firstname from the mycontacts table


named as a

b.Profession -- to fetch column called profession from the profession table


named as b

from MY_CONTACTS as a --here mycontacts is used as first table

inner join -- to return matching records only from both the tables--

PROFESSION as b -- here mycontacts is used as second table

on a.Contact_Id = b.Prof_Id; --here based on two primary keys from those tables, the
tables are connected

select a.Last_Name, a.First_Name, a.Email, b.Profession


from MY_CONTACTS as a
inner join PROFESSION as b
on a.Prof_Id = b.Prof_Id;

select a.Last_Name, a.First_Name, b.Status


from MY_CONTACTS as a
inner join STATUS as b
on a.Status_Id = b.Status_id;
select a.Last_Name, a.First_Name, b.State
from MY_CONTACTS as a
inner join ZIP_CODE as b
on a.Zip_Code = b.Zip_Code;

You might also like