0% found this document useful (0 votes)
3 views3 pages

SQL Syntax

The document outlines basic SQL commands for managing database tables, including creating, altering, and deleting tables. It also covers data retrieval using SELECT statements, filtering with WHERE clauses, and joining tables. Additionally, it explains data modification commands and aggregate functions for analyzing data.
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)
3 views3 pages

SQL Syntax

The document outlines basic SQL commands for managing database tables, including creating, altering, and deleting tables. It also covers data retrieval using SELECT statements, filtering with WHERE clauses, and joining tables. Additionally, it explains data modification commands and aggregate functions for analyzing data.
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/ 3

CREATE TABLE: Creates a new table.

CREATE TABLE table_name ( column1 datatype, column2 datatype, ... );

ALTER TABLE: Modifies an existing table (e.g., add/remove columns).

ALTER TABLE table_name ADD column_name datatype;


ALTER TABLE table_name DROP COLUMN column_name;

DROP TABLE: Deletes an entire table.


DROP TABLE table_name;

TRUNCATE TABLE: Deletes all rows from a table but keeps its structure.
TRUNCATE TABLE table_name;

PRIMARY KEY: Uniquely identifies each record in a table.


CREATE TABLE table_name (
column1 datatype PRIMARY KEY,
column2 datatype);

FOREIGN KEY: Ensures data integrity between tables.

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
FOREIGN KEY (column1) REFERENCES other_table(column));

UNIQUE: Ensures all values in a column are unique.

CREATE TABLE table_name (


column1 datatype UNIQUE);

NOT NULL: Ensures a column cannot have NULL values.

CREATE TABLE table_name (


column1 datatype NOT NULL);

DEFAULT: Sets a default value for a column.

CREATE TABLE table_name (


column1 datatype DEFAULT value);

SELECT: Retrieves data from a database.

SELECT column1, column2 FROM table_name;


SELECT * FROM table_name; -- Selects all columns

WHERE: Filters records based on a specified condition.

SELECT column1, column2 FROM table_name WHERE condition;

AND, OR, NOT: Logical operators for filtering.

SELECT * FROM table_name WHERE condition1 AND condition2;


SELECT * FROM table_name WHERE condition1 OR condition2;
SELECT * FROM table_name WHERE NOT condition;

ORDER BY: Sorts results in ascending or descending order.

SELECT column1, column2 FROM table_name ORDER BY column1 ASC;


SELECT column1, column2 FROM table_name ORDER BY column1 DESC;

LIMIT: Limits the number of rows returned.

SELECT column1, column2 FROM table_name LIMIT number;

Joining Tables

INNER JOIN: Selects records with matching values in both tables.

SELECT columns FROM table1 INNER JOIN table2 ON table1.column =


table2.column;

LEFT JOIN: Returns all records from the left table and matching records from the right
table.

SELECT columns FROM table1 LEFT JOIN table2 ON table1.column =


table2.column;

RIGHT JOIN: Returns all records from the right table and matching records from the left
table.

SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column =


table2.column;

FULL JOIN: Returns all records when there is a match in either left or right table.

SELECT columns FROM table1 FULL JOIN table2 ON table1.column =


table2.column;

4. Modifying Data

INSERT INTO: Inserts new records into a table.

INSERT INTO table_name (column1, column2) VALUES (value1, value2);


UPDATE: Updates existing records in a table.
UPDATE table_name SET column1 = value1 WHERE condition;
DELETE: Deletes records from a table.

DELETE FROM table_name WHERE condition;

NB/-

Aggregating Data

COUNT, SUM, AVG, MIN, MAX: Aggregate functions to calculate values.

SELECT COUNT(column_name) FROM table_name;


SELECT AVG(column_name) FROM table_name;
SELECT SUM(column_name) FROM table_name;

GROUP BY: Groups rows with the same values in specified columns.

SELECT column1, COUNT(*) FROM table_name GROUP BY column1;

HAVING: Filters groups created by GROUP BY.

SELECT column1, COUNT(*) FROM table_name GROUP BY column1 HAVING


COUNT(*) > 1;

You might also like