0% found this document useful (0 votes)
134 views6 pages

SQL Select Statement: SELECT FROM Customers WHERE Country 'Mexico'

This document provides an overview of common SQL statements and clauses including SELECT, WHERE, ORDER BY, INSERT, UPDATE, DELETE, JOIN, GROUP BY and more. It describes the syntax for each statement and gives examples of how to use SQL to query, manipulate and manage data in database tables.

Uploaded by

sornakumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
134 views6 pages

SQL Select Statement: SELECT FROM Customers WHERE Country 'Mexico'

This document provides an overview of common SQL statements and clauses including SELECT, WHERE, ORDER BY, INSERT, UPDATE, DELETE, JOIN, GROUP BY and more. It describes the syntax for each statement and gives examples of how to use SQL to query, manipulate and manage data in database tables.

Uploaded by

sornakumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

Sql Select Statement

SELECT column1, column2, ...


FROM table_name;

Sql Select Distinct Statement


SELECT DISTINCT column1, column2, ...
FROM table_name;
Example: SELECT DISTINCT Country FROM Customers;

Sql Where Clause


SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example: SELECT * FROM Customers WHERE Country='Mexico';

SQL AND, OR and NOT Operators


AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

SQL ORDER BY Keyword


SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

SQL INSERT INTO Statement


INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

INSERT INTO table_name


VALUES (value1, value2, value3,...);

SQL NULL Values


IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
IS NOT NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

SQL UPDATE Statement


UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

SQL DELETE Statement


DELETE FROM table_name WHERE condition;

SQL TOP, LIMIT or ROWNUM Clause


The SQL SELECT TOP Clause
SQL Server / MS Access Syntax:
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;

MySQL Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;

Oracle Syntax:
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
SQL MIN() and MAX() Functions
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;

MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;

SQL COUNT(), AVG() and SUM() Functions


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;

SQL LIKE Operator


LIKE Syntax

SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern;

SQL IN Operator
IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
or
ELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

SQL BETWEEN Operators


BETWEEN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

SQL Aliases
Alias Column Syntax
SELECT column_name AS alias_name
FROM table_name;

Alias Table Syntax


SELECT column_name(s)
FROM table_name AS alias_name;

SQL JOIN
 (INNER) JOIN: Returns records that have matching values in both tables
 LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from
the right table
 RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from
the left table
 FULL (OUTER) JOIN: Return all records when there is a match in either left or right table
SQL INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;

SQL LEFT JOIN Syntax


SELECT column_name(s)
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;

SQL RIGHT JOIN Syntax


SELECT column_name(s)
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;

SQL FULL OUTER JOIN Syntax


SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;

SQL Self JOIN


SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

SQL UNION Operator


UNION Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

UNION ALL Syntax


SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;

Sql GROUP BY Syntax (Group functions (COUNT, MAX, MIN, SUM, AVG)
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

SQL HAVING Clause


HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SQL EXISTS Operator
EXISTS Syntax
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);

SQL ANY and ALL Operators


ANY Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name FROM table_name WHERE condition);

ALL Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name FROM table_name WHERE condition);

SQL SELECT INTO Statement


SELECT INTO Syntax
Copy all columns into a new table:
SELECT *
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;

Copy only some columns into a new table:


SELECT column1, column2, column3, ...
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;

SQL INSERT INTO SELECT Statement


INSERT INTO SELECT Syntax
Copy all columns from one table to another table:
INSERT INTO table2
SELECT * FROM table1
WHERE condition;

Copy only some columns from one table into another table:
INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

SQL CREATE TABLE Statement


Syntax:
CREATE TABLE table_name(column1 datatype,column2 datatype,column3 datatype,....);

SQL DROP TABLE Statement


Syntax
DROP TABLE table_name;

SQL ALTER TABLE Statement


ALTER TABLE - ADD Column
ALTER TABLE table_name ADD column_name datatype;

ALTER TABLE - DROP COLUMN


ALTER TABLE table_name DROP COLUMN column_name;

ALTER TABLE - ALTER/MODIFY COLUMN


ALTER TABLE table_name MODIFY column_name datatype;

SQL Constraints
The following constraints are commonly used in SQL:
 NOT NULL - Ensures that a column cannot have a NULL value
 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table
 FOREIGN KEY - Uniquely identifies a row/record in another table
 CHECK - Ensures that all values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column when no value is specified
 INDEX - Used to create and retrieve data from the database very quickly

SQL Create Constraints


CREATE TABLE table_name (column1 datatype constraint, column2 datatype constraint,
column3 datatype constraint,....);

SQL NOT NULL Constraint

SQL NOT NULL on CREATE TABLE

CREATE TABLE Persons(ID int NOT NULL,LastName varchar(255) NOT NULL,


FirstName varchar(255) NOT NULL,Age int);
SQL NOT NULL on ALTER TABLE

ALTER TABLE Persons MODIFY Age int NOT NULL;

You might also like