0% found this document useful (0 votes)
246 views11 pages

Mysql Cheat Sheet

Uploaded by

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

Mysql Cheat Sheet

Uploaded by

Padmasri E
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

 SQL keywords are not case sensitive ie., select and SELECT are the same.

 A semicolon is mandatory at the end of each SQL statement.

Important SQL Commands:

COMMAND DESCRIPTION

SELECT extracts data from a database

UPDATE updates data in a database

DELETE deletes data from a database

INSERT INTO inserts new data into a database

CREATE DATABASE creates a new database

ALTER DATABASE modifies a database

CREATE TABLE creates a new table

ALTER TABLE modifies a table

DROP TABLE deletes a table

CREATE INDEX creates an index

DROP INDEX deletes an index


QUERIES

QUERY DESCRIPTION OUTPUT

SELECT Displays the mentioned fields


of all the records in the table
emp_id,emp_name

FROM EMPLOYEE ;
SELECT * FROM Displays all the fields of all the
records in the table
EMPLOYEE ;

SELECT DISTINCT Displays the unique values in


the mentioned fields of all the
dept_id
records in the table
FROM EMPLOYEE ;

SELECT emp_name Filters the records based on


the condition. Only those
FROM EMPLOYEE
records that satisfy the
WHERE dept_id=210 ; condition mentioned in the
WHERE clause is displayed
SELECT emp_name Displays all the records in
which all the
FROM EMPLOYEE WHERE dept_id=210 AND e
conditions separated
mp_id=101;
by AND are TRUE.

SELECT emp_name Displays a record if any of the


conditions separated by OR is
FROM EMPLOYEE
TRUE.
WHERE dept_id=210 OR emp_id=101;
SELECT emp_name Displays a record if the
condition(s) is NOT TRUE.
FROM EMPLOYEE

WHERE NOT dept_id=210;

SELECT emp_id, emp_name Displays the records arranged


in ascending order of values in
FROM EMPLOYEE
the mentioned column.
ORDER BY emp_name ASC ;
SELECT emp_id, emp_name FROM EMPLOYEE Displays the records arranged
in descending order of values
ORDER BY emp_name DESC ;
in the mentioned column.

SELECT emp_name Displays the records with NULL


value in the mentioned
FROM EMPLOYEE
column.
WHERE emp_name IS NULL ;

SELECT emp_name Displays the records with non-


NULL value in the mentioned
FROM EMPLOYEE
column.
WHERE emp_name IS NOT NULL ;
UPDATE EMPLOYEE Modifies the existing records
in the table.
SET emp_name = ‘ HIJ’

WHERE emp_id=105;

Table after executing the statement:

DELETE FROM EMPLOYEE Deletes existing records in the


table.
WHERE emp_id=105 ;

Table after executing the statement:


INSERT INTO EMPLOYEE Inserts new records into the
table.
VALUES ( 105,’PQR’,210 );

Table after executing the statement:


SELECT MAX( emp_id ) Returns the largest value in the
mentioned column.
FROM EMPLOYEE

WHERE dept_id=210 ;

SELECT MIN( emp_id ) Returns the smallest value in


the mentioned column.
FROM EMPLOYEE

WHERE dept_id=210 ;

SELECT COUNT ( * ) Returns the number of rows


that match the specified
FROM EMPLOYEE
condition.
WHERE dept_id=210 ;
SELECT column1, column2, ... Used to search for a specific
pattern.
FROM table_name
 The percent sign (%)
WHERE columnN LIKE pattern ;
represents zero, one, or
multiple characters

 The underscore sign (_)


represents one, single
character

SELECT column_name(s) The ‘IN’ operator is used to


specify multiple values in the
FROM table_name
where clause.
WHERE column_name IN
It is a shorthand for multiple
( value1 , value2 , ...);
OR conditions.
SELECT column_name(s) Returns records that have
matching values in both the
FROM table1,table2
tables.
WHERE table1.column_name=table2.column_
name ;

SELECT column_name(s) Groups rows that have the


same values into summary
FROM table_name
statements.
WHERE condition
The ‘group by’ statement is
GROUP BY column_name(s); often used with aggregate
functions like count(), sum(),
max(), min(), avg()

SELECT column_name(s) The ‘having clause’ is used to


filter the output of the ‘group
FROM table_name
by’ clause by a specific
WHERE condition condition.

GROUP BY column_name(s)

HAVING condition;

You might also like