SQL Queries
SQL Queries
SQL Queries
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
Query:
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
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).
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.
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';
Query:
Query:
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';