Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

SQL CREATE DATABASE Statement

Syntax:
CREATE DATABASE databasename;
Query:
CREATE DATABASE testDB;
SQL DROP DATABASE Statement
Syntax:
DROP DATABASE databasename;
Query:
DROP DATABASE testDB;
SQL CREATE TABLE Statement
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
.... );
Query:
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255));
Example:
Persons
Create Table Using Another Table
Syntax
CREATE TABLE new_table_name AS
SELECT column1, column2,...
FROM existing_table_name
WHERE ....;
Query:
CREATE TABLE Customers AS
SELECT FirstNaame, Address
FROM Persons;
Example:
Customers

DROP TABLE Statement


Syntax:
DROP TABLE table_name;
Query:
DROP TABLE Persons;
SQL TRUNCATE TABLE
Syntax:
TRUNCATE TABLE table_name;
Query:
TRUNCATE TABLE Persons;
SQL ALTER TABLE Statement
 ALTER TABLE - ADD Column
Syntax:
ALTER TABLE table_name
ADD column_name datatype;
Query:
ALTER TABLE Customers
ADD Email varchar(255);
Customers
First Name Address Email

 ALTER TABLE - DROP COLUMN


Syntax
ALTER TABLE table_name
DROP COLUMN column_name;
Query:
ALTER TABLE Customers
DROP COLUMN Email;
Customers

SQL INSERT INTO Statement


Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...)
Query:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
SQL UPDATE Statement
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Query:
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

SQL SELECT Statements:


If you want to select all the fields available in the table, use the following syntax:
Syntax
SELECT * FROM table_name;
Example:
SELECT * FROM Customers;
SELECT SPECIFIC:
Syntax:
SELECT column1, column2, ...
FROM table_name;
Query:
SELECT CustomerName, City FROM Customers;

The SQL SELECT DISTINCT Statement


The SELECT DISTINCT statement is used to return only distinct (different) values.
Syntax
SELECT DISTINCT column1, column2, ...
FROM table_name;
Query:
SELECT DISTINCT Country FROM Customers;

SELECT COUNT(DISTINCT Country) FROM Customers;

SELECT Count(*) AS DistinctCountries


FROM (SELECT DISTINCT Country FROM Customers);
SQL WHERE Clause

The WHERE clause is used to filter records.

Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
The WHERE clause is not only used in SELECT statement, it is also used in UPDATE,
DELETE statement, etc.!
Query:
SELECT * FROM Customers
WHERE Country='Mexico';

SQL requires single quotes around text values (most database systems will also allow double
quotes).

However, numeric fields should not be enclosed in quotes:

SELECT * FROM Customers


WHERE CustomerID=1;
The SQL AND, OR and NOT Operators

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
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Query:
SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';

OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Query:
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
Query:
SELECT * FROM Customers
WHERE NOT Country='Germany';

Combining AND, OR and NOT

You can also combine the AND, OR and NOT operators.

Query:

SELECT * FROM Customers


WHERE Country='Germany' AND (City='Berlin' OR City='München');

Query:
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';

You might also like