0% found this document useful (0 votes)
12 views4 pages

SQL Cheat Code

This document provides a comprehensive cheat sheet for MySQL database management, covering commands for creating and managing databases and tables, querying data, and using various SQL clauses like WHERE, LIMIT, ORDER BY, and JOINs. It includes practical examples for creating a database, inserting data, and performing aggregate functions. Additionally, it discusses foreign keys, cascading updates, and table alteration commands.

Uploaded by

Aryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

SQL Cheat Code

This document provides a comprehensive cheat sheet for MySQL database management, covering commands for creating and managing databases and tables, querying data, and using various SQL clauses like WHERE, LIMIT, ORDER BY, and JOINs. It includes practical examples for creating a database, inserting data, and performing aggregate functions. Additionally, it discusses foreign keys, cascading updates, and table alteration commands.

Uploaded by

Aryan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

--- DBMS MySQL Cheat Codes ----

# DATABASE RELATED QUERIES ----

CREATE DATABASE <db_name>;

USE <db_name>;

DROP DATABASE <DB_NAME>;

SHOW DATABASES;

SHOW TABLES;

# TABLE RELATED QUERIES ---

1) CREATE TABLE

CREATE TABLE <table_name> (


column_name1 datatype constraint,
column_name2 datatype constraint,
);

CREATE TABLE students (


roll_no INT PRIMARY KEY,
name VARCHAR(50)
);

2) SELECT AND VIEW COMMAND

SELECT * FROM students;

3) INSERT COMMAND

INSERT INTO student(roll_no, name) VALUES (101, "Aryan"), (102, "Arjun");

Q. Practice Question

CREATE DATABASE pvgcoet;

USE pvgcoet;

CREATE TABLE student (


rollno INT PRIMARY KEY,
name VARCHAR(50),
marks INT NOT NULL,
grade VARCHAR(1),
city VARCHAR(20)
);

INSERT INTO student (rollno , name , marks , grade , city)


VALUES
(101,"Aryan",78,"C","Pune"),
(102,"Bhumika",93,"A","Mumbai"),
(103,"Chetan", 85,"B","Mumbai"),
(104,"Dhruv",96,"A","Delhi"),
(105,"Emanuel",82,"B","Delhi"),
(106,"Farah",82,"B","Delhi");
SELECT name , marks FROM student;

SELECT DISTINCT city FROM student;

# WHERE CLAUSE

SELECT * FROM student WHERE marks > 80;


SELECT * FROM student WHERE city = "Mumbai";
SELECT * FROM student WHERE marks > 80 AND city = "Mumbai";
SELECT * FROM student WHERE marks BETWEEN 80 AND 90;
SELECT * FROM student WHERE city IN ("Delhi","Mumbai"); ---(Matches any value in
list)
SELECT * FROM student WHERE city NOT IN ("Delhi","Mumbai");

# LIMIT CLAUSE

SELECT * FROM student LIMIT 3; (print only 3 rows)

# ORDER BY CLAUSE

SELECT * FROM student ORDER BY city ASC; (ASC / DESC)


SELECT * FROM student ORDER BY marks DESC LIMIT 3; (Top 3)

# AGGREGATE FUNCTIONS

SELECT MAX(marks) FROM student;


SELECT AVG(marks) FROM student;

# GROUP BY CLAUSE

SELECT city , COUNT(name) FROM student GROUP BY city;

SELECT city, AVG(marks) FROM student GROUP BY city ORDER BY city ASC;

SELECT mode , count(customer) FROM payment GROUP BY mode;

# HAVING CLAUSE

SELECT city , count(id) FROM student GROUP BY city HAVING MAX(marks) > 90;

# GENERAL ORDER

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

# UPDATE IN TABLES

UPDATE student SET grade = "O" WHERE name = "Aryan";


UPDATE student SET marks = marks + 1;

# DELETE IN TABLES

DELETE FROM student WHERE marks < 33;


# VISITING FOREIGN KEYS

CREATE TABLE dept (


id INT PRIMARY KEY,
name VARCHAR(50)
);

CREATE TABLE teacher (


id INT PRIMARY KEY,
name VARCHAR(50),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES dept(id)
);

# CASCADING FOREIGN KEYS

CREATE TABLE student (


id INT PRIMARY KEY,
courseID INT,
FOREIGN KEY (courseID) REFERENCES course(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);

# TABLE ALTER QUERIES

ADD Column

ALTER TABLE table_name


ADD COLUMN column_name datatype constraint;

2. DROP Column

ALTER TABLE table_name


DROP COLUMN column_name;

3. RENAME Table

ALTER TABLE table_name


RENAME TO new_table_name;

4. TRUNCATE TABLE (To delete table's data)

TRUNCATE TABLE table_name;

# JOINS IN SQL

Inner Join --- Gives Common Data

SELECT column(s)
FROM table_A
INNER JOIN table_B
ON tableA.col_name = tableB.col_name;

2. Left Join

SELECT column(s)
FROM table_A
LEFT JOIN table_B
ON tableA.col_name = tableB.col_name;

3. Right Join

SELECT column(s)
FROM table_A
RIGHT JOIN table_B
ON tableA.col_name = tableB.col_name;

4. Full Join

SELECT column(s)
FROM table_A
LEFT JOIN table_B
ON tableA.col_name = tableB.col_name;

UNION

SELECT column(s)
FROM table_A
RIGHT JOIN table_B
ON tableA.col_name = tableB.col_name;

You might also like