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

My SQL Queries Guide

This document serves as a quick reference guide for MySQL queries, covering essential database and table operations, CRUD operations, and various SQL clauses. It includes examples for creating, altering, and dropping databases and tables, as well as performing data manipulation and retrieval using SELECT, INSERT, UPDATE, and DELETE commands. Additionally, it addresses advanced topics like joins, grouping, indexing, stored procedures, and views.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

My SQL Queries Guide

This document serves as a quick reference guide for MySQL queries, covering essential database and table operations, CRUD operations, and various SQL clauses. It includes examples for creating, altering, and dropping databases and tables, as well as performing data manipulation and retrieval using SELECT, INSERT, UPDATE, and DELETE commands. Additionally, it addresses advanced topics like joins, grouping, indexing, stored procedures, and views.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MySQL Queries - Quick Reference Guide

1. Database Operations

• Create Database:

CREATE DATABASE mydb;

• Use Database:

USE mydb;

• Drop Database:

DROP DATABASE mydb;

2. Table Operations

• Create Table:

CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE,
age INT
);

• Drop Table:

DROP TABLE users;

• Alter Table (Add Column):

ALTER TABLE users ADD COLUMN city VARCHAR(100);

• Alter Table (Modify Column):

ALTER TABLE users MODIFY age SMALLINT;

1
3. CRUD Operations

• Insert Data:

INSERT INTO users (name, email, age) VALUES ('John Doe', 'john@example.com',
30);

• Select Data:

SELECT * FROM users;

• Select Specific Columns:

SELECT name, age FROM users;

• Update Data:

UPDATE users SET age = 35 WHERE name = 'John Doe';

• Delete Data:

DELETE FROM users WHERE name = 'John Doe';

4. WHERE Clause and Conditions

• Basic Condition:

SELECT * FROM users WHERE age > 25;

• Multiple Conditions:

SELECT * FROM users WHERE age > 25 AND city = 'New York';

• IN and BETWEEN:

SELECT * FROM users WHERE age IN (25, 30);


SELECT * FROM users WHERE age BETWEEN 20 AND 35;

2
5. Sorting & Limiting

• Order By:

SELECT * FROM users ORDER BY age DESC;

• Limit:

SELECT * FROM users LIMIT 5;

6. Aggregate Functions

• Count, Sum, Avg, Min, Max:

SELECT COUNT(*), AVG(age), MIN(age), MAX(age) FROM users;

7. Joins

• Inner Join:

SELECT u.name, o.amount


FROM users u
JOIN orders o ON u.id = o.user_id;

• Left Join:

SELECT u.name, o.amount


FROM users u
LEFT JOIN orders o ON u.id = o.user_id;

• Right Join:

SELECT u.name, o.amount


FROM users u
RIGHT JOIN orders o ON u.id = o.user_id;

3
8. Grouping Data

• Group By:

SELECT city, COUNT(*) FROM users GROUP BY city;

• Having Clause:

SELECT city, COUNT(*) as total FROM users GROUP BY city HAVING total > 2;

9. Indexes & Constraints

• Create Index:

CREATE INDEX idx_email ON users(email);

• Add Unique Constraint:

ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email);

10. Stored Procedures & Views

• Create Stored Procedure:

DELIMITER //
CREATE PROCEDURE GetUsers()
BEGIN
SELECT * FROM users;
END //
DELIMITER ;

• Create View:

CREATE VIEW user_view AS SELECT name, age FROM users;

Let me know if you'd like this as a PDF or extended with advanced MySQL operations like triggers,
transactions, or full-text search.

You might also like