Sql
Sql
WHERE Syntax
FROM table_name
WHERE condition;
SELECT * FROM Customers
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one
condition:
● The AND operator displays a record if all the conditions separated by AND
are TRUE.
● The OR operator displays a record if any of the conditions separated by OR
is TRUE.
● The NOT operator displays a record if the condition(s) is NOT TRUE.
AND Syntax
FROM table_name
OR Syntax
FROM table_name
NOT Syntax
FROM table_name
The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
2. If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of
the values is in the same order as the columns in the table. Here, the INSERT
INTO syntax would be as follows:
We will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT column_names
FROM table_name
SELECT column_names
FROM table_name
UPDATE Syntax
UPDATE table_name
WHERE condition;
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;
The MySQL DELETE Statement
The DELETE statement is used to delete existing records in a table.
DELETE Syntax
The LIMIT clause is useful on large tables with thousands of records. Returning
a large number of records can impact performance.
LIMIT Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;
The MAX() function returns the largest value of the selected column.
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
SELECT MAX(column_name)
FROM table_name
WHERE condition;
COUNT() Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
● (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
CREATION OF DATABASE:
EG:
3)DROP TABLE:
The DROP TABLE statement is used to drop an existing table in a database.
Syntax
4)TRUNCATE:
The TRUNCATE TABLE statement is used to delete the data inside a table, but
not the table itself.
5) ALTER TABLE:
The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.
The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
EXAMPLE:
ALTER TABLE Customers
ADD Email varchar(255);
EXAMPLE:
ALTER TABLE Customers
DROP COLUMN Email;