1. CLICK HERE TO RUN SQL COMMANDS
2. CLICK HERE TO RUN SQL COMMANDS
3. CLICK HERE TO RUN JOIN QUERIES
Click Here to download SQL Notes
____________________________________________________________________________________
DROP TABLE IF EXISTS contacts;
CREATE TABLE contacts (
id INT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(25) NOT NULL,
email VARCHAR(210) NOT NULL,
age VARCHAR(22) NOT NULL);
INSERT INTO contacts VALUES (1,'Kavin','Peterson','kavin.peterson@verizon.net','21');
INSERT INTO contacts VALUES(2,'Nick','Jonas','nick.jonas@me.com','18');
INSERT INTO contacts VALUES(3,'Peter','Heaven','peter.heaven@google.com','23');
INSERT INTO contacts VALUES(4,'Michal','Jackson','michal.jackson@aol.com','22');
INSERT INTO contacts VALUES(5,'Sean','Bean','sean.bean@yahoo.com','23');
INSERT INTO contacts VALUES(6,'Tom ','Baker','tom.baker@aol.com','20');
INSERT INTO contacts VALUES(7,'Ben','Barnes','ben.barnes@comcast.net','17');
INSERT INTO contacts VALUES(8,'Mischa ','Barton','mischa.barton@att.net','18');
INSERT INTO contacts VALUES(9,'Sean','Bean','sean.bean@yahoo.com','16');
INSERT INTO contacts VALUES(10,'Eliza','Bennett','eliza.bennett@yahoo.com','25');
INSERT INTO contacts VALUES(11,'Michal','Krane','michal.Krane@me.com','25');
INSERT INTO contacts VALUES(12,'Peter','Heaven','peter.heaven@google.com','20');
INSERT INTO contacts VALUES(13, 'Brian','Blessed','brian.blessed@yahoo.com','20');
SELECT * FROM contacts
ORDER BY id;
/*find duplicate values*/
SELECT
email, COUNT(email)
FROM
contacts
GROUP BY
email
HAVING
COUNT (email) > 1;
_________________________________________________________________________________
DROP TABLE IF EXISTS student;CREATE TABLE student( id integer primary key, name text,
branch text, date_of_reg date, percentage float,
sem year);
INSERT INTO student VALUES (1602053001,'A','CSE','2018-01-03',5.6,5);
INSERT INTO student VALUES (1602053002,'B','EC','2018-01-04',7.5,5);
INSERT INTO student VALUES (1602053003,'C','EE','2018-01-06',5.8,5);
INSERT INTO student VALUES (1602053004,'D','CSE','2018-01-03',5.5,5);
INSERT INTO student VALUES (1602053005,'E','SE','2018-01-03',8.5,5);
INSERT INTO student VALUES (1602053006,'F','CSE',null,5.4,5);
INSERT INTO student VALUES (1602053007,'G','CE','2018-01-07',6.5,5);
INSERT INTO student VALUES (1602053008,'H','CSE','2018-01-03',5.9,5);
INSERT INTO student VALUES (1602053009,'I','CE','2018-01-08',7.5,5);
INSERT INTO student VALUES (1602053010,'J','CE','2018-01-03',5.0,5);
INSERT INTO student VALUES (1602053011,'K',null,'2018-01-03',5.5,5);
INSERT INTO student VALUES (1602053012,'L','ME','2018-01-09',8.5,5);
INSERT INTO student VALUES (1602053013,'M','CSE','2018-01-03',5.2,5);
INSERT INTO student VALUES (1602053014,'N','ME','2018-01-03',4.5,5);
INSERT INTO student VALUES (1602053015,'O','CSE','2018-01-03',5.1,5);
INSERT INTO student VALUES (1602053016,'P','CSE','2018-01-03',5.6,5);
INSERT INTO student VALUES (1602053017,'Q','ME','2018-01-09',7.5,5);
INSERT INTO student VALUES (1602053018,'R','CSE','2018-01-03',5.8,5);
INSERT INTO student VALUES (1602053019,'S','CSE','2018-01-03',5.4,5);
INSERT INTO student VALUES (1602053020,'T','EE','2018-01-02',5.0,5);
INSERT INTO student VALUES (1602053021,'P','CSE','2018-01-03',5.3,5);
INSERT INTO student VALUES (1602053022,'C','EE','2018-01-06',3.5,5);
/* 1.Select record of all students from student table*/
SELECT * FROM student;
/* 2.Select id,name from student table*/
SELECT id,name FROM student;
/* 3.Select distinct id,name from student table*/
SELECT distinct id,name FROM student;
/* 4.Count distinct id,name from student table*/
SELECT COUNT(DISTINCT id) FROM student;
/* 5.Select all students from student table where date_of_reg='2018-01-03'*/
SELECT * FROM student WHERE date_of_reg='2018-01-03';
/* 6.Select DISTINCT id,NAME,DATE_OF_REG from student table where date_of_reg='2018-01-03'*/
SELECT DISTINCT id,NAME,DATE_OF_REG FROM student WHERE date_of_reg='2018-01-03';
/* 7.Select all students from student table where BRANCH='CSE'AND DATE_OF_REG='2018-01-03'AND PERCENTAGE=5.6*/
SELECT * FROM STUDENT WHERE BRANCH='CSE'AND DATE_OF_REG='2018-01-03'AND PERCENTAGE=5.6;
/* 8.Select all students from student table where BRANCH='CSE'OR DATE_OF_REG='2018-01-03' OR PERCENTAGE=5.6*/
SELECT * FROM STUDENT WHERE BRANCH='CSE'OR DATE_OF_REG='2018-01-03' OR PERCENTAGE=5.6;
/* 9.Select all students from student table where not BRANCH='CSE'OR DATE_OF_REG='2018-01-03' OR PERCENTAGE=5.6*/
SELECT * FROM STUDENT WHERE not (BRANCH='CSE'OR DATE_OF_REG='2018-01-03' OR PERCENTAGE=5.6);
/* 10.Select all students from student table ordered by branch name*/
SELECT * FROM STUDENT order by branch ;
/* 11.Select all students from student table ordered by branch name in descending order*/
SELECT * FROM STUDENT order by branch desc;
/* 12.Select all students from student table ordered by date_of_reg in ascending order*/
SELECT * FROM STUDENT order by date_of_reg asc;
/* 13.Insert values student table and display them*/
INSERT INTO student VALUES (1602053100,'Lateral Entry','CSE','2018-01-03',5.6,5);
SELECT * FROM student;
/* 14.Insert values student table where id is not null and display them*/
INSERT INTO student VALUES (1602053101,'Lateral Entry2',null ,'2018-01-03',5.6,5);
SELECT * FROM student;
/* 15.Select null values from student table */
SELECT * FROM student where branch is null;
/* 16.Select null values from student table */
SELECT * FROM student where branch is not null;
/* 17.Update the record of roll number 1602053100 from student table */
update student set name='changed' , branch='CSE' where id=1602053100;
SELECT * FROM student;
/* 18.Delete the record of roll number 1602053100 from student table */
delete from student where id=1602053100;
SELECT * FROM student;
____________________________________________________________________________________
DROP TABLE IF EXISTS student;
CREATE TABLE student( id integer, name text,
branch text, date_of_reg date, percentage float,
sem year);
INSERT INTO student VALUES (1602053001,'A','CSE','2018-01-03',5.6,5);
INSERT INTO student VALUES (1602053002,'B','EC','2018-01-04',7.5,5);
INSERT INTO student VALUES (1602053003,'C','EE','2018-01-06',5.8,5);
INSERT INTO student VALUES (1602053004,'D','CSE','2018-01-03',5.5,5);
INSERT INTO student VALUES (1602053005,'E','SE','2018-01-03',8.5,5);
INSERT INTO student VALUES (1602053006,'F','CSE','2018-01-03',5.4,5);
INSERT INTO student VALUES (1602053007,'G','CE','2018-01-07',6.5,5);
INSERT INTO student VALUES (1602053008,'H','CSE','2018-01-03',5.9,5);
INSERT INTO student VALUES (1602053009,'I','CE','2018-01-08',7.5,5);
INSERT INTO student VALUES (1602053010,'J','CE','2018-01-03',5.0,5);
INSERT INTO student VALUES (1602053011,'K','CSE','2018-01-03',5.5,5);
INSERT INTO student VALUES (1602053012,'L','ME','2018-01-09',8.5,5);
INSERT INTO student VALUES (1602053013,'M','CSE','2018-01-03',5.2,5);
INSERT INTO student VALUES (1602053014,'N','ME','2018-01-03',4.5,5);
INSERT INTO student VALUES (1602053015,'O','CSE','2018-01-03',5.1,5);
INSERT INTO student VALUES (1602053016,'P','CSE','2018-01-03',5.6,5);
INSERT INTO student VALUES (1602053017,'Q','ME','2018-01-09',7.5,5);
INSERT INTO student VALUES (1602053018,'R','CSE','2018-01-03',5.8,5);
INSERT INTO student VALUES (1602053019,'S','CSE','2018-01-03',5.4,5);
INSERT INTO student VALUES (1602053020,'T','EE','2018-01-02',5.0,5);
INSERT INTO student VALUES (1602053021,'P','CSE','2018-01-03',5.3,5);
INSERT INTO student VALUES (1602053022,'C','EE','2018-01-06',3.5,5);
SELECT * FROM student;
SELECT DISTINCT branch,percentage FROM student;
SELECT DISTINCT name,percentage FROM student;
SELECT COUNT (DISTINCT id) FROM student;
SELECT COUNT (DISTINCT name) FROM student;
SELECT * FROM student WHERE date_of_reg='2018-01-03';
SELECT DISTINCT id,NAME,DATE_OF_REG FROM student WHERE date_of_reg='2018-01-03';
SELECT * FROM STUDENT WHERE BRANCH='CSE'AND DATE_OF_REG='2018-01-03'AND PERCENTAGE=5.6;
SELECT * FROM STUDENT WHERE BRANCH='CSE'OR DATE_OF_REG='2018-01-03' OR PERCENTAGE=5.6;
SELECT ID,NAME FROM STUDENT ORDER BY ID ASC;
SELECT ID,NAME FROM STUDENT ORDER BY ID DESC;
_____________________________________________________________________________________
OUTPUT
| id | name | branch | date_of_reg | percentage | sem |
|---|---|---|---|---|---|
| 1602053001 | A | CSE | 2018-01-03 | 5.6 | 5 |
| 1602053002 | B | EC | 2018-01-04 | 7.5 | 5 |
| 1602053003 | C | EE | 2018-01-06 | 5.8 | 5 |
| 1602053004 | D | CSE | 2018-01-03 | 5.5 | 5 |
| 1602053005 | E | SE | 2018-01-03 | 8.5 | 5 |
| 1602053006 | F | CSE | 2018-01-03 | 5.4 | 5 |
| 1602053007 | G | CE | 2018-01-07 | 6.5 | 5 |
| 1602053008 | H | CSE | 2018-01-03 | 5.9 | 5 |
| 1602053009 | I | CE | 2018-01-08 | 7.5 | 5 |
| 1602053010 | J | CE | 2018-01-03 | 5 | 5 |
| 1602053011 | K | CSE | 2018-01-03 | 5.5 | 5 |
| 1602053012 | L | ME | 2018-01-09 | 8.5 | 5 |
| 1602053013 | M | CSE | 2018-01-03 | 5.2 | 5 |
| 1602053014 | N | ME | 2018-01-03 | 4.5 | 5 |
| 1602053015 | O | CSE | 2018-01-03 | 5.1 | 5 |
| 1602053016 | P | CSE | 2018-01-03 | 5.6 | 5 |
| 1602053017 | Q | ME | 2018-01-09 | 7.5 | 5 |
| 1602053018 | R | CSE | 2018-01-03 | 5.8 | 5 |
| 1602053019 | S | CSE | 2018-01-03 | 5.4 | 5 |
| 1602053020 | T | EE | 2018-01-02 | 5 | 5 |
| 1602053021 | P | CSE | 2018-01-03 | 5.3 | 5 |
| 1602053022 | C | EE | 2018-01-06 | 3.5 | 5 |
| branch | percentage |
|---|---|
| CSE | 5.6 |
| EC | 7.5 |
| EE | 5.8 |
| CSE | 5.5 |
| SE | 8.5 |
| CSE | 5.4 |
| CE | 6.5 |
| CSE | 5.9 |
| CE | 7.5 |
| CE | 5 |
| ME | 8.5 |
| CSE | 5.2 |
| ME | 4.5 |
| CSE | 5.1 |
| ME | 7.5 |
| CSE | 5.8 |
| EE | 5 |
| CSE | 5.3 |
| EE | 3.5 |
| name | percentage |
|---|---|
| A | 5.6 |
| B | 7.5 |
| C | 5.8 |
| D | 5.5 |
| E | 8.5 |
| F | 5.4 |
| G | 6.5 |
| H | 5.9 |
| I | 7.5 |
| J | 5 |
| K | 5.5 |
| L | 8.5 |
| M | 5.2 |
| N | 4.5 |
| O | 5.1 |
| P | 5.6 |
| Q | 7.5 |
| R | 5.8 |
| S | 5.4 |
| T | 5 |
| P | 5.3 |
| C | 3.5 |
| COUNT (DISTINCT id) |
|---|
| 22 |
| COUNT (DISTINCT name) |
|---|
| 20 |
| id | name | branch | date_of_reg | percentage | sem |
|---|---|---|---|---|---|
| 1602053001 | A | CSE | 2018-01-03 | 5.6 | 5 |
| 1602053004 | D | CSE | 2018-01-03 | 5.5 | 5 |
| 1602053005 | E | SE | 2018-01-03 | 8.5 | 5 |
| 1602053006 | F | CSE | 2018-01-03 | 5.4 | 5 |
| 1602053008 | H | CSE | 2018-01-03 | 5.9 | 5 |
| 1602053010 | J | CE | 2018-01-03 | 5 | 5 |
| 1602053011 | K | CSE | 2018-01-03 | 5.5 | 5 |
| 1602053013 | M | CSE | 2018-01-03 | 5.2 | 5 |
| 1602053014 | N | ME | 2018-01-03 | 4.5 | 5 |
| 1602053015 | O | CSE | 2018-01-03 | 5.1 | 5 |
| 1602053016 | P | CSE | 2018-01-03 | 5.6 | 5 |
| 1602053018 | R | CSE | 2018-01-03 | 5.8 | 5 |
| 1602053019 | S | CSE | 2018-01-03 | 5.4 | 5 |
| 1602053021 | P | CSE | 2018-01-03 | 5.3 | 5 |
| id | name | date_of_reg |
|---|---|---|
| 1602053001 | A | 2018-01-03 |
| 1602053004 | D | 2018-01-03 |
| 1602053005 | E | 2018-01-03 |
| 1602053006 | F | 2018-01-03 |
| 1602053008 | H | 2018-01-03 |
| 1602053010 | J | 2018-01-03 |
| 1602053011 | K | 2018-01-03 |
| 1602053013 | M | 2018-01-03 |
| 1602053014 | N | 2018-01-03 |
| 1602053015 | O | 2018-01-03 |
| 1602053016 | P | 2018-01-03 |
| 1602053018 | R | 2018-01-03 |
| 1602053019 | S | 2018-01-03 |
| 1602053021 | P | 2018-01-03 |
| id | name | branch | date_of_reg | percentage | sem |
|---|---|---|---|---|---|
| 1602053001 | A | CSE | 2018-01-03 | 5.6 | 5 |
| 1602053016 | P | CSE | 2018-01-03 | 5.6 | 5 |
| id | name | branch | date_of_reg | percentage | sem |
|---|---|---|---|---|---|
| 1602053001 | A | CSE | 2018-01-03 | 5.6 | 5 |
| 1602053004 | D | CSE | 2018-01-03 | 5.5 | 5 |
| 1602053005 | E | SE | 2018-01-03 | 8.5 | 5 |
| 1602053006 | F | CSE | 2018-01-03 | 5.4 | 5 |
| 1602053008 | H | CSE | 2018-01-03 | 5.9 | 5 |
| 1602053010 | J | CE | 2018-01-03 | 5 | 5 |
| 1602053011 | K | CSE | 2018-01-03 | 5.5 | 5 |
| 1602053013 | M | CSE | 2018-01-03 | 5.2 | 5 |
| 1602053014 | N | ME | 2018-01-03 | 4.5 | 5 |
| 1602053015 | O | CSE | 2018-01-03 | 5.1 | 5 |
| 1602053016 | P | CSE | 2018-01-03 | 5.6 | 5 |
| 1602053018 | R | CSE | 2018-01-03 | 5.8 | 5 |
| 1602053019 | S | CSE | 2018-01-03 | 5.4 | 5 |
| 1602053021 | P | CSE | 2018-01-03 | 5.3 | 5 |
| id | name |
|---|---|
| 1602053001 | A |
| 1602053002 | B |
| 1602053003 | C |
| 1602053004 | D |
| 1602053005 | E |
| 1602053006 | F |
| 1602053007 | G |
| 1602053008 | H |
| 1602053009 | I |
| 1602053010 | J |
| 1602053011 | K |
| 1602053012 | L |
| 1602053013 | M |
| 1602053014 | N |
| 1602053015 | O |
| 1602053016 | P |
| 1602053017 | Q |
| 1602053018 | R |
| 1602053019 | S |
| 1602053020 | T |
| 1602053021 | P |
| 1602053022 | C |
| id | name |
|---|---|
| 1602053022 | C |
| 1602053021 | P |
| 1602053020 | T |
| 1602053019 | S |
| 1602053018 | R |
| 1602053017 | Q |
| 1602053016 | P |
| 1602053015 | O |
| 1602053014 | N |
| 1602053013 | M |
| 1602053012 | L |
| 1602053011 | K |
| 1602053010 | J |
| 1602053009 | I |
| 1602053008 | H |
| 1602053007 | G |
| 1602053006 | F |
| 1602053005 | E |
| 1602053004 | D |
| 1602053003 | C |
| 1602053002 | B |
| 1602053001 | A |
No comments:
Post a Comment