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

SQL Notes

Uploaded by

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

SQL Notes

Uploaded by

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

COMMON QUERIES:

SHOW DATABASES; ;

DROP TABLE table_name;

DROP DATABASE database_name;

SELECT * FROM table_name;


or
SELECT rollno,name FROM students;

DEFAULT - Value get added if not mention ("Eg. column_name INT DEFAULT 25000;")

UNIQUE - Unique value and Null value are allowed (Eg. UNIQUE (email_id) used in
create table)

DISTINCT - Unique Value (Eg.SELECT DISTINCT city FROM students;)

WHERE - To apply any condition (Eg. SELECT * FROM students WHERE city="Mumbai";)

AND - To add one more condition (Eg. SELECT * FROM students WHERE city="Mumbai" AND
marks > 80; )

Arithmetic Operators : +,-,*,/,%

Comparison Operators : =,!=,>,>=,,<=

Logical Operators : AND,OR,NOT,BETWEEN,ALL,LIKE,ANY (Eg. SELECT * FROM students


WHERE marks BETWEEN 80 AND 90;)

Bitwise Operators : &(Bitwise AND),|(Bitwise OR)

LIMIT Clause : controls the number of row to be displayed in a table (Eg. SELECT *
FROM students LIMIT 3;)

ASC and DSC : Ascending and descending order (Eg. SELECT * FROM students ORDER BY
city ASC;)

GROUP BY clause : Makes group of city and it also use Aggregate Clause for the
count (Eg.SELECT city, COUNT(city) FROM students GROUP BY city;)

GENERAL ORER:

SELECT column(s)
FROM table_name
WHERE condition
GROUP BY column(s)
HAVING condition
ORDER BY column(s) ASC;

Aggregate Function:

COUNT()
MAX()
MIN()
AVG()
SUM()
KEYS:

Primary key - "Unique value are present but null value not allowed"
foreign Key - "Primary key of other table, Duplicate and null value allowed"

CREATE QUERY:

CREATE DATABASES database_name;


USE database_name - "To use the database"

CREATE TABLE table_name(


col_name col_datatype constraints,
col_name col_datatype constraints,
);

INSERT INTO employee


(id,name)
VALUES
(1, "Dheeraj"),
(2, "Varun");

-"To add values in table"

UPDATE students
SET marks = 92
WHERE marks = 12;

-"To Update values in a column"

SET SQL_SAFE_UPDATES = 0; - If any error while using UPDATE to off just add 1 here

DELETE FROM students


WHERE marks<50;

-"To Delete any value from the table"

DELETE FROM students

-"To Delete all value from the table"

FOREIGN KEY (dpt_id) REFERENCES dpt(id)


-"To link two tables"

ON UPDATE CASCADE
ON DELETE CASCADE

-"To make perform update and delete operation simultaneously on both table"

ALTER TABLE mytb


ADD COLUMN age INT;

-"To add column to the table"

ALTER TABLE mytb


DROP COLUMN age;

-"To delete the column"

ALTER TABLE example


RENAME TO mytb;

-"To rename the table"

ALTER TABLE mytb


CHANGE COLUMN id my_id INT;

-"To Rename the Column"

ALTER TABLE mytb


MODIFY name VARCHAR(100);

-"To change the datatype and constraint of the table"

JOINS:

Inner join:

SELECT *
FROM mytb as tb
INNER JOIN mytb1 as tb1
ON tb1.id = tb.id;

-"Common data (intersection)"

left join:

SELECT *
FROM mytb as tb
LEFT JOIN mytb1 as tb1
ON tb.id = tb1.id;
-"Left entire data and common data"

Right join:

SELECT *
FROM mytb as tb
RIGHT JOIN mytb1 as tb1
ON tb.id = tb1.id;

-"Right entire data and common data"

Union join:

SELECT *
FROM mytb as tb
LEFT JOIN mytb1 as tb1
ON tb.id = tb1.id
UNION
SELECT *
FROM mytb as tb
RIGHT JOIN mytb1 as tb1
ON tb.id = tb1.id;

-"combination of left and right data"

left exclusive join:

SELECT *
FROM mytb as tb
LEFT JOIN mytb1 as tb1
ON tb.id=tb1.id
WHERE tb.id IS NULL;

-"entire left data without common data"

Right exclusive join:

SELECT *
FROM mytb as tb
RIGHT JOIN mytb1 as tb1 //usin as it is calld alias
ON tb.id=tb1.id
WHERE tb.id IS NULL;

-"entire Right data without common data"

Self join:

SELECT *
FROM mytb as tb
JOIN mytb1 as tb1
ON tb.id = tb1.id;

-"Just like inner join but it is used to compare two table for same kind based on
the similar column they share"

UNION:

SELECT name FROM mytb


UNION
SELECT surr FROM mytb1;

"Give all values of table1 = name column and table1 = surr column all unique"

UNION ALL:

SELECT name FROM mytb


UNION ALL
SELECT surr FROM mytb1;

-"Give all values of table1 = name column and table1 = surr column including
duplicate"

SQL SUB QUERIES:

three ways of writing:


SELECT
FROM
WHERE (mostly used)

WHERE:
SELECT name, marks
FROM students
WHERE marks IN (SELECT marks
FROM students
WHERE marks%2=0);

FROM:
SELECT MAX(marks)
FROM (SELECT * FROM students WHERE city = "Delhi") as temp;

SELECT:

SELECT (SELECT MAX(marks) FROM students), name


FROM students;

-"Can be used but not used mostly"(SELECT)

-"Using query logic in other query and it is used for single query which has only
one specific value not for the whole column value"(SELECT,FROM and WHERE)
MySQL View:

CREATE VIEW viewing AS


SELECT name,rollno,city FROM students;

SELECT * FROM viewing; (any condition can be applied like this


"SELECT * FROM view WHERE marks>90;")

DROP VIEW viewing; (View can be dropped)

-"It decide which data should be visible in a table by creating a virtual view of
the table"

You might also like