Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Untitled

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

-- Anything start with -- in SQL consider as comment and will not execute

drop database trendytech;

create database trendytech;

use trendytech;

show tables;

create table trendytech.students (

student_id int auto_increment,

student_fname varchar(30) NOT NULL,

student_mname varchar(30),

student_lname varchar(30) NOT NULL,

student_email varchar(30) NOT NULL,

student_phone varchar(15) NOT NULL,

student_alternate_phone varchar(15),

enrollment_date timestamp not null,

years_of_exp int not null,

student_company varchar(30),

batch_date varchar(30) not null,

source_of_joining varchar(30),

location varchar(30) not null default 'Bangalore',

primary key (student_id)

);

desc students;

select * from students;


-- It's not good practise in production to always drop the existing cable so whenever any changes
needed to made in table structure/schema I'll use ALTER command

-- Alter table two columns for email to set it UNIQUE and for timestamp to reflect current timestamp

alter table students change student_email student_email varchar(30) NOT NULL unique key;

alter table students change enrollment_date enrollment_date timestamp not null default
current_timestamp;

-- Insert some data into student table

-- For any column which is set as NULL means it can accept NULL value now when you entering the
data in table for this column some records may have values

-- and some records may not have values in that case mention that column name in insert command
and give the value where you have to mention and where you don't need to mention any value you
can give NULL

insert into students (student_fname, student_lname, student_email, student_phone, years_of_exp,


student_company, batch_date, source_of_joining)values

('Rohit','Sharma','rohit@gmail.com','+917838285358','5','Wipro','05-02-2022','Linkedin'),

('Kapil','Gupta','kapil@gmail.com','+917828596345','7','TCS','07-03-2022','Naukri.com'),

('Govinda','Kapoor','govinda.kapoor@yahoo.com','+919485452453','3','Coforge','05-02-2022','Friend
Reference'),

('Ram','Srivastava','ram.srivastava@gmail.com','+917838295328','5','Wipro','09-03-2022','Linkedin'),

('Mohit','Sharma','mohit@gmail.com','+917838285388','5','Wipro','05-03-2022','Naukri.com'),

('Rajnesh','Sharma','rajnesh123@gmail.com','+917838244358','5','Walmart','05-02-2021','Friend
Reference'),

('Rohit','Gupta','rohit12@gmail.com','+917118285358','5','Wipro','05-02-2022','Linkedin');

-- Let's do few updates

select * from students;

update students set student_mname = 'Lakhan' where student_fname = 'Ram';

select * from students where student_fname = 'Rajnesh';


update students set student_alternate_phone = '+91456528532' where student_fname = 'Rajnesh';

-- Let's Insert some more records

insert into students (student_fname, student_mname, student_lname, student_email,


student_phone, student_alternate_phone, years_of_exp, student_company, batch_date,
source_of_joining, location) values

('Dravid', 'Das', 'Gupta', 'dravid.123@yahoo.com','+91464466546',


'+91545453435','7','Facebook','05-07-2022',NULL,'Jodhpur'),

('Dravid', NULL , 'Srivastava', 'srivastava.d@yahoo.com','+91464466646',


'+9131566553435','13','Google','11-09-2022',NULL,'Lucknow'),

('Mohammad', 'Azhrudin', 'Khan', 'khan.azr@rediff.com','+91464461414', '+91545431005','7','S&P


Global','15-11-2022',NULL,'Kashipur'),

('Malik', 'Rohit', 'Sharma', 'Sharma.azr@rediff.com','+91464461414', NULL,'7','IHSMarkit','03-11-


2022','Friend Reference','Delhi'),

('Shashank', NULL, 'Seth', 'shashankseth040@rediff.com','+91464461414', NULL,'7','S&P Global','15-


11-2022','Linkedin','Baghpat'),

('Aprajita', NULL, 'Seth', 'aprajita@rediff.com','+91464461414', null,'7',NULL,'15-11-


2022',NULL,'Noida'),

('Shashank', NULL, 'Seth', 'shashankseth37@rediff.com','+91464461414', NULL,'7',null,'15-11-


2022',NULL,'Muzzafarnagar');

insert into students (student_fname, student_mname, student_lname, student_email,


student_phone, student_alternate_phone, years_of_exp, student_company, batch_date,
source_of_joining, location, enrolled_course) values

('Sanjeev', NULL, 'Seth', 'shashankseth307@rediff.com','+91464461414', NULL,'7',null,'15-11-


2022','Friend Reference','Delhi', 1),

('Samanvay', NULL, 'Shukla', 'samanvay307@rediff.com','+91464461414', NULL,'7',null,'15-11-


2022','Friend Reference','Delhi', 1),

('Sidharth', NULL, 'Singh', 'sidharth@rediff.com','+91464461414', NULL,'7',null,'15-11-2022','Friend


Reference','Bangalore', 2),

('Sadhu', NULL, 'Singh', 'sidhart12h@rediff.com','+91464461414', NULL,'7',null,'15-11-2022','Friend


Reference','Bangalore',5),

('Shiva', NULL, 'Singh', 's2h@rediff.com','+91464461414', NULL,'7',null,'15-11-2022','Friend


Reference','Bangalore',4),
('Chavi', NULL, 'Singh', 'chavi@rediff.com','+91464461414', NULL,'7',null,'15-11-
2022','Linkedin','Bangalore',3),

('Charu', NULL, 'Gupta', 'charu@rediff.com','+91464461414', NULL,'7',null,'15-11-


2022','Linkedin','Bangalore',2),

('Shuchi', NULL, 'Mishra', 'shuchi@rediff.com','+91464461414', NULL,'7',null,'15-11-


2022','Naukri.com','Bangalore',5),

('Khyati', NULL, 'Mishra', 'khyati@rediff.com','+91464461414', NULL,'7',null,'15-11-


2022','Naukri.com','Mumbai',3),

('Saroj', NULL, 'Singh', 'sarojh@rediff.com','+91464461414', NULL,'7',null,'15-11-2022','Friend


Reference','Mumbai',4);

-- Now earlier our enrolled_course column was selected to varchar but we making this coloumn as
foreign key from parent table course column course_id so we need to change it to integer type

alter table students drop column enrolled_course;

select * from students;

alter table students add column enrolled_course int not null;

-- Let's create the course table

create table course (

course_id int auto_increment,

course_name varchar(20) not null,

course_duration varchar(10) not null,

primary key (course_id),

unique key (course_name)

);

-- Let's add one more column in course table as course_fees

alter table course add column course_fees bigint not null;

desc course;
insert into course (course_name, course_duration, course_fees) values

('BigData Engineer', '6 months', 60000),

('Azure Architect', '4 months', 40000),

('AWS Cloud Architect', '4 months', 50000),

('Azure Data Engineer', '4 months', 75000),

('AWS Devops', '4 months',55000),

('Web Development', '4 months', 40000);

-- Here the catch is if there is already data in course_id in course table but as of now the
enrolled_course column in the student table is blank system will not accept this command and it will
throw error

-- Error Code:1452 cannot update or add a child row: a foreign key constraint fails

-- So to overcome this error first you will disable the foreign key checks by running the below
command

set foreign_key_checks = 0;

-- (0 means disabled 1 means enabled if want to enable again then you can run it again by giving 1)

-- Now after running this command run the above command 2 again it will work

-- Now you want to modify/enter the values in multiple rows for students for enrolled_course field
so to modify values in multiple rows you'll run the below command

update students

set enrolled_course = (case when student_id = 1 then 3

when student_id = 2 then 1

when student_id = 3 then 1

when student_id = 4 then 3

when student_id = 5 then 2

when student_id = 6 then 3


when student_id = 7 then 6

when student_id = 8 then 4

when student_id = 9 then 5

when student_id = 10 then 4

when student_id = 11 then 4

when student_id = 12 then 3

when student_id = 13 then 5

when student_id = 14 then 6

end)

where student_id in (1,2,3,4,5,6,7,8,9,10,11,12,13,14);

-- Now if you keep the command

set foreign_key_checks = 0;

-- Like this only when you will do insert for any student with enrolled_course id out of range in
course id it will still accept because this check is disable

-- so its advisable to enable it again you can try by inserting any column in student table with course
id out of range see it will accept that so enable this again

set foreign_key_checks = 1;

-- After enabling this delete the record you entered above with out of range course_id to verify this
check with false course_id to maintain the reference integrity of the table.

-- To know more about reference integrity of tables in relational DBMS refer this IBM block

-- https://www.ibm.com/docs/en/informix-servers/14.10?topic=integrity-referential

-- Now when you'll enter any record which is out of range of course_id in course table it will throw
error
-- Let's do some more entry in student table to understand the wildcard character %\

insert into students (student_fname, student_lname, student_email, student_phone, years_of_exp,


batch_date, location, enrolled_course) values

('Roh%it', 'Kumar' , '123@gmail.com', '845646546', '12', '7-12-2022', 'Ahemdabad', 3),

('%ohit', 'Yadav' , '12345@gmail.com', '845646546', '12', '7-12-2022', 'Gurgaon', 3),

('Rohit%', 'Bhardwaj' , '123rohit@gmail.com', '845646546', '12', '7-12-2022', 'Gurgaon', 3),

('Roh%%it', 'Khana' , '123mo@gmail.com', '845646546', '12', '7-12-2022', 'Ahemdabad', 3);

insert into students (student_fname, student_lname, student_email, student_phone, years_of_exp,


batch_date, location, enrolled_course) values

('Sam', 'Kumar' , '123sam@gmail.com', '845646546', '12', '7-12-2022', 'Ahemdabad', 3),

('Maxs', 'Kumar' , 'maxzsam@gmail.com', '845646546', '12', '7-12-2022', 'Kanpur', 3);

select student_fname from students where student_fname like '%aji%' ;

select student_fname from students where student_fname like 'sha%' ;

select student_fname from students where student_fname like 'M%' ;

select student_fname from students where student_fname like 'MO%' ;

select student_fname from students where student_fname like '%hit' order by student_fname;

select student_fname from students where student_fname like '_____' ;

select student_fname from students where student_fname like '__ash%' ;

select student_fname from students where student_fname like '%\%';

select student_fname from students where student_fname like '%\%%';

select student_fname from students where student_fname like '%s%';

select student_fname from students where student_fname like '____\%%';

delete from students where student_fname like '%\%%';

--Let;s update default location Bangalore of few students in one go


update students set location =

(case when student_id = 2 then 'Delhi'

when student_id = 3 then 'Mumbai'

when student_id = 4 then 'Chandigarh'

end)

where student_id in (2,3,4);

-- Let's alter the course_fees coloumn in course table to decimal, I have mentioned (7,2) because
there are already records in my course fees coloumn which is of 5 decimal places so if i'll give

-- any other value it will throw error

alter table course change course_fees fees decimal(7,2) not null;

-- Let's insert few decimal records

insert into course (course_id, course_name, course_duration, fees) values (12, 'DataBricks Engineer',
'5 months', 75000.23);

select * from course where course_id = 12;

insert into course (course_id, course_name, course_duration, course_fees) values (13, 'GCP
Engineer', '5 months', 75000.23);

insert into course (course_id, course_name, course_duration, course_fees) values (14, 'AWS
Associate', '5 months', 75000.235); -- This will truncat the last 5 as only two decimal degits are allows

insert into course (course_id, course_name, course_duration, course_fees) values (15, 'Azure
Associate', '5 months', 75555.52);

insert into course (course_id, course_name, course_duration, course_fees) values (16, 'Azure
Associate', '5 months', 755558.52); -- This will throw error as there are more then 5 digits before
decimal

-- Let's alter table for two more coloumn one for entry time of record and one for update time of
record, let;s do this on seperate coloumns

alter table course add column Entry_Time timestamp default current_timestamp;


-- The above command will add the default timestamp on all the already existing records and if you
want to change any you can change them by giving time in proper format

update course set Entry_Time = '2023-01-09 06:14:35' where course_id = 1;

update course set Entry_Time = '2023-01-09 06:14:35' where course_id = 3;

-- adding one more coloumn to see the update logs

alter table course add column Updatetime timestamp on update current_timestamp;

-- Let;s insert few records and lets update few records

insert into course (course_id, course_name, course_duration, course_fees) values

(16, 'DevOps', '4 months', 35000),

(17, 'JAVA Developer', '4 months', 44000);

update course set course_name = 'Full Stack Dev' where course_id = 17;

update course set course_duration = '3 months' where course_id = 3;

-- Now verify the changes

select * from course;

-- Select query for logical operators;

select * from students where location = 'Bangalore';

select * from students where location != 'Bangalore';

select * from course where course_name like '%data%';

select * from course where course_name not like '%data%';


select student_fname, student_lname, location, years_of_exp, source_of_joining from students

where (location = 'Bangalore' and source_of_joining = 'Linkedin' and years_of_exp < 8) ;

-- See below when to use or and and

-- and when I want students between 5 and 12 years of experience

-- or when I want students with less then 5 and greater then 12

select student_fname, student_lname, location, years_of_exp from students

where (years_of_exp > 5 and years_of_exp < 12) order by years_of_exp desc;

select student_fname, student_lname, location, years_of_exp from students

where (years_of_exp < 5 or years_of_exp > 12) order by years_of_exp desc;

select * from students where years_of_exp between 5 and 10;

select * from students where years_of_exp not between 5 and 10;

select student_fname, student_lname, student_company, years_of_exp from students where


student_company in ('Facebook','S&P Global','IHSMarkit', 'Coforge')

order by student_company, years_of_exp;

select student_fname, student_lname, student_company, years_of_exp from students where


student_company not in ('Facebook','S&P Global','IHSMarkit', 'Coforge') \

order by student_company, years_of_exp;

-- To classify course as Diploma and Masters we first need to change course_duration coloumn from
varchar to int but before changing coloumns we have to change values

-- from 4 months to 4, 6 months to 6 like this so that when we modify coloumn to int type it should
not give error due to miss match values

update course set course_duration = (CASE

when course_id = 1 then 6

when course_id = 2 then 5


when course_id = 3 then 7

when course_id = 4 then 4

when course_id = 5 then 3

when course_id = 6 then 2

when course_id = 7 then 8

when course_id = 8 then 3

when course_id = 9 then 6

when course_id = 10 then 6

when course_id = 11 then 3

when course_id = 12 then 4

when course_id = 13 then 5

when course_id = 14 then 2

when course_id = 15 then 4

when course_id = 16 then 5

when course_id = 17 then 6

END)

where course_id in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17);

-- Now let's just change the coloumn type to int

alter table course change course_duration course_duration_months int not null;

-- NOw let's do Case loop on classifying as degree and diploma

select course_id, course_name, CASE

when course_duration_months >= 6 then 'Masters Programme'

else 'Diploma Certificate'

END as Degree_Type

from course;

select count(course_id) as 'Number of courses', CASE


when course_duration_months >= 6 then 'Masters Programme'

else 'Diploma Certificate'

END as Degree_Type

from course group by Degree_Type;

select student_id, student_fname, student_lname, location, student_company, CASE

WHEN student_company in ('Wipro', 'TCS', 'Coforge') then 'Service Based Company'

else 'Product Based Company'

END as Company_Type

from students;

select count(student_id), CASE

WHEN student_company in ('Wipro', 'TCS', 'Coforge') then 'Service Based Company'

WHEN student_company is NULL then 'No Company'

else 'Product Based Company'

END as Company_Type

from students GROUP BY company_type;

-- INNER JOIN for only matching records

select students.student_id, students.student_fname, students.student_lname, course.course_name


from students

inner join course on students.enrolled_course = course.course_id;

-- To create duplicate table with all the records without table schema/structure

create table latest_students as select * from students;

desc latest_students;

-- To copy the exact table schema from original table, it will only copy the schema but no records
create table duplicate_students like students;

desc duplicate_students;

-- To copy records from original students table to this new duplicate students table for specific
coloumns

-- Please makesure we have to enter the records for all those coloumns which are set to not null
otherwise it will throw the error

insert into duplicate_students (student_id, student_fname, student_lname, student_email,


student_phone, enrollment_date, years_of_exp, batch_date, enrolled_course)

select student_id, student_fname, student_lname, student_email, student_phone, enrollment_date,


years_of_exp, batch_date, enrolled_course from students;

-- To copy all the records in all the coloumns exactly as original table

create table students2 like students;

insert into students2 select * from students;

--

You might also like