SQL Commands
SQL Commands
Submitted by:
Udit Narain Gupta (24205309)
Nelson Kujur (24205303)
The CREATE TABLE statement in SQL is used to define a new table in a database. It specifies
the table name, the columns, and their respective data types. Tables are fundamental to relational
databases, as they store structured data in rows and columns.
1. PRIMARY KEY:
a. Ensures that each value in the column is unique and not null.
2. NOT NULL:
a. Prevents null values in a column.
3. UNIQUE:
a. Ensures all values in a column are distinct.
4. CHECK:
a. Enforces a condition on the values (e.g., CHECK (salary > 0)).
5. FOREIGN KEY:
a. Links a column to another table to maintain relationships.
Syntax
sql
The INSERT statement in MySQL is used to add new rows (records) into a table in a database. It
allows data entry into specific or all columns of the table.
Syntax of INSERT
Basic Syntax
sql
Copy code
INSERT INTO table_name (column1, column2, ..., columnN)
VALUES (value1, value2, ..., valueN);
Syntax
DESCRIBE table_name;
Example
Table Tasks:
SQL Command
sql
DESCRIBE tasks;
Output
4. RENAME Command
The RENAME TABLE statement in MySQL is used to change the name of a table. This
command is particularly useful when restructuring databases or changing table names to align
with updated naming conventions or project requirements.
Syntax
Sql
Command
sql
RENAME TABLE tasks TO project_tasks;
Output
Syntax
sql
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC];
Command
Sql
SELECT *
FROM project_tasks
ORDER BY status;
Syntax
Sql
SELECT column1, column2, ...
FROM table_name
WHERE condition;
● condition can involve comparisons (e.g., =, >, <, !=), logical operators (AND, OR), or
patterns (e.g., LIKE).
Usage Example : SELECT with WHERE Used to filter records and retrieve only
those that meet the condition.
Example:
sql
SELECT *
FROM project_tasks
WHERE assigned_to = 'Charlie';
Table: employees
Sql
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
sql
UPDATE project_tasks
SET status = 'Completed'
WHERE project_id = 2;
Table before UPDATE:
Table: employees
The GROUP BY command in SQL is used to group rows that have the same values in specified
columns into summary rows, like "grouping by department" or "grouping by customer." It is
commonly used with aggregate functions (like COUNT, SUM, AVG, MAX, MIN) to perform
calculations on each group of rows.
Syntax
SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name;
Sql
Output:
The HAVING command in MySQL is used to filter the results of a query based on aggregate
functions (like COUNT, SUM, AVG, etc.) after the GROUP BY operation. While WHERE is
used to filter rows before grouping, HAVING is used to filter groups after aggregation.
Syntax
sql
Usage Example
Command:
sql
SELECT status, COUNT(task_id) AS total_tasks
FROM project_tasks
GROUP BY status
HAVING COUNT(task_id) > 1;
The LIMIT command in MySQL is used to restrict the number of rows returned by a query. It is
especially useful for pagination or when you only need a subset of data.
Syntax
sql
SELECT column1, column2, ...
FROM table_name
LIMIT number_of_rows;
Usage Examples
Example:
sql
SELECT *
FROM project_tasks
LIMIT 2;
Explanation:
● This query will return the first 2 rows from the project_tasks table.
Table: employees
Output: