SQL Practice
SQL Practice
Show databases;
Use trendytech;
Select database();
Show tables;
DDL – Data Definition Language - deals with table structure – Create , Alter , Drop
DML – Data Manipulation Language - deals with the data directly – Insert. Update, Delete
INSERT INTO employee (fname, lname, salary ) VALUES ( “Akash”,”Gupta” 20 ); - better way to insert
values
INSERT INTO employee (fname, salary ) VALUES ( “Akash”, 20 ); - when one field is missing
INSERT INTO employee (fname, lname, salary ) VALUES ( “Akash”,”Gupta” 20 ),(“”, “”, 34 ) ; - Multiple
value inputs
Create table employee ( fname varchar(20) NOT NULL ,mname varchar(20), lname varchar(20) NOT
NULL, salary int NOT NULL); - defining what can’t be NULL
Create table employee ( fname varchar(20) NOT NULL ,mname varchar(20), lname varchar(20) NOT
NULL, salary int NOT NULL, location varchar(20) DEFAULT “bangalore”) – defining default values if
any value is not provided
Create table employee ( fname varchar(20) NOT NULL ,mname varchar(20), lname varchar(20) NOT
NULL, salary int NOT NULL, location varchar(20) NOT NULL DEFAULT “bangalore”) – can’t explicitly
pass NULL as value after this combination
Session 3 – Primary key / auto increment keys / unique key
Create table employee ( id int PRIMARY KEY, fname varchar(20) NOT NULL ,mname varchar(20),
lname varchar(20) NOT NULL, salary int NOT NULL, location varchar(20) NOT NULL DEFAULT
“bangalore”);- PRIMARY KEY – Null and repeated values are not allowed
Create table employee ( id int, fname varchar(20) NOT NULL ,mname varchar(20), lname varchar(20)
NOT NULL, salary int NOT NULL, location varchar(20) NOT NULL DEFAULT “bangalore”, PRIMARY
KEY(id)); - other way to define primary key
Create table employee ( id int AUTO_INCREMENT, fname varchar(20) NOT NULL ,mname
varchar(20), lname varchar(20) NOT NULL, salary int NOT NULL, location varchar(20) NOT NULL
DEFAULT “bangalore”, PRIMARY KEY(id)); - to autoincrement a particular value and not enter again
UNIQUE KEY – it can hold NULL value, and can have multiple NULL values- considers it different,
Only one primary key eg. ID, but we can have multiple UNIQUE keys eg. Email, phone no
Create table employee ( id int UNIQUE KEY, fname varchar(20) NOT NULL ,mname varchar(20),
lname varchar(20) NOT NULL, salary int NOT NULL, location varchar(20) NOT NULL DEFAULT
“bangalore”, PRIMARY KEY(id));
Session 4 –
Select * from employee where fname=”manish”; - this stmnt will also fetch “Manish” in results( case
– insensitive)
Select * from employee where binary fname=”manish”; - this statement now became case sensitive
and will not fetch “Manish”
Select fname as name , lname as surname from employee; - Alias use ‘as’ to change column name
Update employee set lname=”Sinha” where fname=”manish”; - update only specific records
Update employee set lname=”Sinha”; - now this will update all records’s lname as Sinha
Truncate internally drops the table and then recreates it that’s why it is a DDL command while Delete
command deletes one entry at a time so it is a DML command. Therefore, truncate is more efficient.
Create table courses(course_id int NOT NULL, course_name varchar(20), course_fees int NOT NULL
PRIMARY KEY(course_id));
F.K constraint is used to prevent actions that would destroy link between two tables.
F.K – It is a field in one table that refers to the primary key in another table
Table with foreign key – child table, table with primary key- parent/ reference table
Select DISTINCT location from students; - only shows distinct cities only once
Select f_name from students ORDER BY years_of_exp; - still arranges in terms of Y.O.E even if not
included in select
Select f_name, years_of_exp from students ORDER BY years_of_exp, student_fname – first it wil
order by Y.O.E and then who have same Y.O.E it will do second level sorting on basis on alphabets
LIMIT
Select * from students limit 3; will show only 3 record (no sense, need to use it with order by)
Select * from students ORDER BY years_of_exp limit 3; (will show 3 candidates with least
experience)
Select * from students ORDER BY years_of_exp DESC limit 3 ( 3 candidates with highest experience)
Select * from students order by enroll_date limit 0,3; – gives first 3 enrolled record(from first record
give 3 record same as limit 3)
Select * from students order by enroll_date limit 3,2; - from 4th record give 2 records
Select * from students where f_name LIKE ‘ra%’; – all name starting with ra
Select * from students where f_name LIKE ‘_____’; – give all names having 5 characters ( 5 _ )
Distinct
Select count( DISTINCT student_company) from students; - no. of different companies students are
coming from
Select count(*) from students where batch_date like ’%-02-%’ ; - people enrolled in any batch of Feb
GROUP BY
Select MIN(Y.O.E), student_fname from students; - This won’t work, MIN is independent, it won’t
know which name to fetch, it will only get the value
Select student_fname from students order by Y.O.E limit 1 – min. experience guy
select source_of_joining, MAX(y.o.e) from students group by source_of_joining; - max y.o.e of each
source of joining seperately
select location, AVG(y.o.e) from students group by location; - average of y.o.e of each location
seperately
Session 11 – Joins
Select course_name from courses where course_id= (select selected_course from students where
student_fname =”Rahul”); - Nested query to find course_name
Create table students_latest as select * from students; - to generate a table from a table
All the matching records + All the non matching records in the right table which does not have the
match in left table padded with NULL’s
All the matching records + non matching records from left + non matching records from right
UNION
CROSS JOIN
Select * from students, courses; - total result : students * courses , all first table records are
matched with all record in second table
Select source_of_joining, count(*) as total from students group by source_of_joining where total>1;
- This will not work because, where clause is used to filter the individual record before aggregation
Select source_of_joining, count(*) as total from students group by source_of_joining having total>1;
Select location, count(*) as total from students where y_o_e >10 GROUP BY location HAVING total >
1;