SQL - MySQL¶
1. Database Commands¶
Create Database¶
Creates a new database.
Example: Creates a database named school.
Show Databases¶
Lists all available databases.
Use Database¶
Switches to a specific database.
Example: Now all operations will be done inside school.
Drop Database¶
Deletes a database permanently.
2. Table Commands¶
Create Table¶
Creates a new table with fields and datatypes.
Example: Creates a students table with ID, Name, Age, and Grade.
Show Tables¶
Lists all tables inside the selected database.
Describe Table¶
Displays table structure (columns, types, keys).
Alter Table¶
Used to modify table structure.
Example: Adds a new column email.
Example: Changes column name size.
Drop Table¶
Deletes a table permanently.
3. CRUD Operations¶
Insert Data¶
Inserts new records.
Select Data¶
Retrieves data from a table.
Example: Selects all records.
Example: Shows only name & age of students with grade 'A'.
Update Data¶
Updates existing records.
Example: Changes grade of student with ID=1 to B.
Delete Data¶
Deletes records from a table.
Example: Removes student with ID=1.
4. Clauses¶
Distinct¶
Removes duplicate values.
Example: Returns unique grades.
Group By¶
Groups rows with same values and used with aggregate functions.
Example: Counts number of students in each grade.
Order By¶
Sorts results in ascending (ASC) or descending (DESC).
Example: Lists students by increasing age.
Having¶
Filters results after grouping.
Example: Shows only grades that have more than 2 students.
5. Operators¶
Logical Operators¶
- LIKE → Pattern matching with wildcards.
Example: Names starting with J.
- AND → Both conditions must be true.
- OR → Either condition can be true.
- NOT → Negates condition.
- BETWEEN → Range of values.
Comparison Operators¶
=: Equal
!=: Not Equal
<, >: Less / Greater
<=, >=: Less or Equal / Greater or Equal
With these commands, you can create, modify, and query databases effectively.