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

All Mysql Queries Cheat Sheet

The document provides a cheat sheet on various MySQL queries including: 1. Inserting values into tables, deleting records, and updating records. 2. Using functions like CONCAT, SUBSTR, and LENGTH to manipulate string values. 3. Filtering records using WHERE clauses, operators like AND and OR, and BETWEEN. 4. Sorting and limiting output with ORDER BY and LIMIT clauses.

Uploaded by

zk032918
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)
42 views

All Mysql Queries Cheat Sheet

The document provides a cheat sheet on various MySQL queries including: 1. Inserting values into tables, deleting records, and updating records. 2. Using functions like CONCAT, SUBSTR, and LENGTH to manipulate string values. 3. Filtering records using WHERE clauses, operators like AND and OR, and BETWEEN. 4. Sorting and limiting output with ORDER BY and LIMIT clauses.

Uploaded by

zk032918
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
You are on page 1/ 8

ALL MYSQL QUERIES CHEAT SHEET

Creating db and tables plus inserting values:


create database zk;
create table R(id int , fname varchar(255),lname varchar(255),salary
decimal(10,3));
select * from R;
insert into R(id,fname,lname,salary)
values
(1,'bisma','khan',23300.00),
(2,'eman','ali',12333.89),
(3,'fahad','mustafa',34562.90),
(4,'iqra','abbas',12345.08),
(5,'ainy','xyz',34340.90),
(6,'NAQASH','posh',2323.009);

1) Insert: Adding new records to a database table.


Specifying column name & values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
If you are adding values for all the columns of the table, you do not need to specify the column names in the
SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here,
the INSERT INTO syntax would be as follows:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
_________________________________________________________________
2) Delete: Removing records from a database table.
The DELETE statement is used to delete existing records in a table:
DELETE FROM table_name WHERE condition;
Example:
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';
To DELETE a single column from whole table:
ALTER TABLE table_name
DROP COLUMN column_name;
To DELETE whole table:
DROP TABLE table_name;
_________________________________________________________________

3) Update: Modifying existing records in a database table.


UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;
Example:
UPDATE Customers
SET PostalCode = 00000
WHERE Country = 'Mexico';
Example:
UPDATE Customers
SET PostalCode = 00000;
Be careful when updating records. If you omit the WHERE clause, ALL records will
be updated!
_________________________________________________________________

4) CONCAT: Combining or concatenating string values.


SELECT CONCAT(first_name, ' ', last_name) AS full_ name
FROM employees;
If you want to add a new column that consists of concatenated values:
alter table F
add column full_name varchar(255);
update F
set full_name = concat (fname,' ',lname);
__________________________________________________________________

5) SUBSTR: Extracting a substring from a string.


SELECT SUBSTR(product_name, 1, 5) AS short_name
FROM products;
OR
When to add a new column in the table of substring:
alter table F
add column test varchar(255);
UPDATE F
SET test = SUBSTRING(fname, 3, 5);
__________________________________________________________________
6) LENGTH: Calculating the length of a string.
SELECT LENGTH (fname) AS string_length
FROM F
where id=1;
OR
SELECT LENGTH (fname) AS string_length
FROM F; ------ to get the length of all the strings
___________________________________________________________________
7) INSTR: Finding the position of a substring within a string.
SELECT INSTR('eman ali ', 'ali') AS position;
___________________________________________________________________
8) LPAD, RPAD: Padding strings with characters on the left or right.
LPAD:
SELECT LPAD(fname , 6, '0') AS padded_string
FROM F
where id =1;
result : 0bisma -> indicates that it will add 0 to right until it make it a 6 alphabet
letter.
RPAD:
SELECT RPAD (id, 6, 'X') AS padded_string
FROM F ;
___________________________________________________________________
9) SELECT statement: Retrieving data from a database table.
SELECT column1, column2, ...
FROM table_name
WHERE condition;
SELECT * FROM employees;
___________________________________________________________________

10) WHERE clause: Filtering records based on specified conditions.


WHERE condition;
SELECT * FROM employees;
___________________________________________________________________

11) AND, OR, NOT operators: Combining conditions for more complex filtering.
AND:
SELECT * FROM R
WHERE LENGTH (fname) = 5 AND salary > 2323;
OR:
SELECT *
FROM R
WHERE id = 1 OR id = 3;
NOT:
SELECT *
FROM employees
WHERE NOT department = 'IT';
___________________________________________________________________

12) ORDER BY clause: Sorting query results.


Descending Order
SELECT *
FROM products
ORDER BY price DESC;
Ascending Order:
SELECT *
FROM R
order by fname ASC;
___________________________________________________________________

13) MIN and MAX functions: Finding the minimum and maximum values in a column.
TO RETRIEVE WHOLE ROW:
SELECT *
FROM employees
WHERE salary = (SELECT MIN(salary) FROM employees);

SELECT *
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);

TO RETRIEVE JUST ONE FIELD VALUE:


SELECT MAX(salary) AS minimum_salary
FROM R;

SELECT MAX (salary) AS minimum_salary


FROM R;
___________________________________________________________________
LIMIT clause: Limiting the number of rows returned by a query.
SELECT * FROM R
LIMIT 30;
return rows max 30
___________________________________________________________________

14) COUNT, AVG, and SUM functions: Calculating counts, averages, and sums.
COUNT:
SELECT COUNT(*) AS total_employees
FROM R;
AVG:
SELECT AVG(salary) AS average_salary
FROM employees;
SUMS:
SELECT SUM(salary) AS total_salaries
FROM R;

___________________________________________________________________

15) LIKE operator: Searching for patterns in strings.(wild cards)


For example, if you want to find all products in a table that contain the word "apple"
anywhere in their names:
SELECT *
FROM products
WHERE product_name LIKE '%apple%';
if you want to find products with names that have exactly six characters and the third
character is the letter "o," you can use:
SELECT *
FROM products
WHERE product_name LIKE’---o--’;
search for a word containing 6 letters where o is the 4th letter.
___________________________________________________________________

IN operator: Checking for values within a specified list.


SELECT *FROM employees
WHERE department IN(‘HR’, ‘Finance’) ;
___________________________________________________________________

16) BETWEEN operator: Selecting values within a range.


In SQL, the BETWEEN operator is used in a WHERE clause to filter rows based on a
specified range of values within a column. It is commonly used with numeric and date
data types to select rows that fall within a particular range.
SELECT * FROM sales
WHERE total_amount BETWEEN 1000 AND 5000;
___________________________________________________________________

17) Alias: Giving a table or column a temporary name in a query.


SELECT e.employee_id, e.employee_name
FROM employees AS e
WHERE e.department = 'HR';
"e" is an alias for the "employees" table, making it easier to reference columns in that
table in the query.
SELECT employee_id AS ID, employee_name AS Name, salary * 12 AS
AnnualSalary
FROM employees;
In this case, column aliases "ID" and "Name" provide more intuitive names for the
columns in the query result, and "AnnualSalary" is an alias for a calculated value.
___________________________________________________________________

18) Drop table: Deleting a table from the database.


DROP TABLE EMPLOYEE;
___________________________________________________________________
Alter table: Modifying the structure of a table.
Already done in previous
___________________________________________________________________

19) Distinct: Retrieving unique values from a column.


SELECT DISTINCT country
FROM cities;
___________________________________________________________________
20) Drop: Deleting a database.
Already done
___________________________________________________________________
21) TOP Keyword: Selecting the top rows from a result set.
SELECT TOP (number) column1, column2, ...
FROM table_name;
not used in mysql!limit is used!
SELECT TOP 10 *
FROM products;
___________________________________________________________________
22) TOP PERCENT Keyword: Selecting the top percentage of rows from a result set.
SELECT TOP 25 PERCENT employee_name, salary
FROM employees
ORDER BY salary DESC;
___________________________________________________________________

23) ROWNUM (Oracle SQL): Pseudocolumn for row numbers in Oracle SQL.
SELECT ROWNUM, first_name, last_name
FROM customers;
___________________________________________________________________

24) [!charlist] Character List: Using regular expressions to filter by character patterns.
select those students whose names doesn’t start from this:
SELECT * FROM Students WHERE StudentName REGEXP '^[^npy]';
select those students whose names start from this:
SELECT *
FROM R
WHERE fname REGEXP '^[npy]';
___________________________________________________________________

Primary key and Foreign Key:

ALTER TABLE Orders


ADD PRIMARY KEY (id);

ALTER TABLE Orders


ADD CONSTRAINT fk_user
FOREIGN KEY (user_id) REFERENCES Users(id);

INSERT INTO Orders (id, user_id, order_date, total_amount)


VALUES (1, 101, '2023-11-05', 150.00),
(2, 102, '2023-11-06', 200.50);
___________________________________________________________________

You might also like