SQL Queries
SQL Queries
Create Table CREATE TABLE table_name (id INT, Creates a new table
name VARCHAR(100));
Insert Data INSERT INTO table_name (id, name) Inserts a new row into the
VALUES (1, 'Alice');
table
Select All Rows SELECT * FROM table_name; Retrieves all rows and
columns
Select Specific SELECT name FROM table_name; Retrieves specific column(s)
Columns
Select with SELECT * FROM table_name WHERE id Filters rows based on a
= 1;
Condition condition
Update Data UPDATE table_name SET name = 'Bob' Updates existing data
WHERE id = 1;
Delete Row DELETE FROM table_name WHERE id = Deletes a row based on
1;
condition
Drop Table DROP TABLE table_name; Deletes the entire table
Truncate Table TRUNCATE TABLE table_name; Removes all rows but keeps
the table structure
Rename Table ALTER TABLE old_name RENAME TO Renames a table
new_name;
Add Column ALTER TABLE table_name ADD age Adds a new column to the
INT;
table
Modify Column ALTER TABLE table_name MODIFY age Modifies column data type
VARCHAR(3); (MySQL)
Drop Column ALTER TABLE table_name DROP COLUMN Removes a column from the
age;
table
Create Index CREATE INDEX idx_name ON Speeds up lookups using the
table_name(name);
name column
Order Results SELECT * FROM table_name ORDER BY Sorts the results
name ASC;
Count Rows SELECT COUNT(*) FROM table_name; Counts the number of rows
Group By SELECT age, COUNT(*) FROM Groups rows by column
table_name GROUP BY age;
Join Tables SELECT * FROM t1 JOIN t2 ON t1.id Joins rows from two tables
= t2.t1_id;