SQL Commands TCS
SQL Commands TCS
Creating Database
Deleting Database
Show Database
SHOW DATABASES;
Create Table
Show Tables
Delete Table
Select distinct
The SELECT DISTINCT statement is used to return only distinct (different) values.
SELECT DISTINCT column1, column2, ...
FROM table_name;
where
The WHERE clause is used to filter records.
OR Operator
NOT Operator
The NOT operator is used in combination with other operators to give the opposite result
Update
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Delete
Limit
The Limit clause is used to specify the number of records to return.
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
NULL Value
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
An aggregate function is a function that performs a calculation on a set of values, and returns a single
value.
Min
SELECT MIN(column_name)
FROM table_name
WHERE condition;
Max
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Count
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
Sum
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Avg
SELECT AVG(column_name)
FROM table_name
WHERE condition;
GROUP BY
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
HAVING Clause
The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate
functions.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
IN Operator
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
NOT IN
SELECT column_name(s)
FROM table_name
WHERE column_name NOT IN (value1, value2, ...);
BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be numbers, text, or
dates.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
-------------------------------Keys--------------------------
Primary key
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single or
multiple columns (fields).
CREATE TABLE City (
ID int NOT NULL,
City_Name varchar(255) NOT NULL,
PRIMARY KEY (ID)
);
Foreign Keys
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in
another table.
ON Delete Cascade
ON Update Cascade
);
Constraints
• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from
the right table
• RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from
the left table
• FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
SQL Subqueries
1) Select
2) From
3) Where
Views
Virtual tables based on the result set , A view will be automatically updated if table is updated
MySQL :: MySQL 8.4 Reference Manual :: 14.7 Date and Time Functions