0% found this document useful (0 votes)
67 views

SQL Quick Reference by Tableau Gurus

This document provides a quick reference for common SQL statements and their usage. It lists statements for querying, modifying, and managing data in databases and tables. Key statements covered include SELECT, INSERT, UPDATE, DELETE, CREATE DATABASE, CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX, and DROP INDEX. It also defines operators, constraints, and SQL clauses like DISTINCT, WHERE, and GROUP BY.

Uploaded by

Reza Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

SQL Quick Reference by Tableau Gurus

This document provides a quick reference for common SQL statements and their usage. It lists statements for querying, modifying, and managing data in databases and tables. Key statements covered include SELECT, INSERT, UPDATE, DELETE, CREATE DATABASE, CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE INDEX, and DROP INDEX. It also defines operators, constraints, and SQL clauses like DISTINCT, WHERE, and GROUP BY.

Uploaded by

Reza Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

SQL QUICK REFERENCE BY TABLEAU GURUS

SQL quick reference


 SELECT - extracts data from a database
 UPDATE - updates data in a database
 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database
 CREATE DATABASE - creates a new database
 ALTER DATABASE - modifies a database
 CREATE TABLE - creates a new table
 ALTER TABLE - modifies a table
 DROP TABLE - deletes a table
 CREATE INDEX - creates an index (search key)
 DROP INDEX - deletes an index

NOTE:-

 SELECT * FROM <table_name>; This is called blind query


 Desc <table Name>; This command is used to find the column name in the
table

Statement Definition Syntax Example


SELECT CustomerName AS
used to temporarily
SELECT column_name AS alias_name Customer, ContactName AS
Aliases rename a table or a
FROM table_name; [Contact Person]
column heading.
FROM Customers;
To add a column in a ALTER TABLE table_name ALTER TABLE Persons
Alter table
table ADD column_name datatype ADD DateOfBirth date
SELECT * FROM Customers
The AND operator WHERE
displays a record if Country='Germany'
both the first condition AND City='Berlin';
AND the second
condition are true. SELECT * FROM Customers
The OR operator WHERE City='Berlin'
AND & OR
displays a record if OR City='München';
either the first
condition OR the SELECT * FROM Customers
second condition is WHERE
true. Country='Germany'
AND (City='Berlin' OR
City='München');
Auto-increment allows
a unique number to be
Auto ALTER TABLE Persons ALTER TABLE Persons
generated when a new
Increment AUTO_INCREMENT=<number> AUTO_INCREMENT=100
record is inserted into a
table.
Between operator selects values SELECT column_name(s) SELECT * FROM Products
SQL QUICK REFERENCE BY TABLEAU GURUS
within a range. The FROM table_name
WHERE Price BETWEEN
operator values can be numbers, WHERE column_name BETWEEN value1 
10 AND 20;
text, or dates. AND value2;
CREATE TABLE Persons
(
P_Id int NOT NULL
CHECK (P_Id>0),
CREATE TABLE table_name
Used to limit the value LastName varchar(255)
Check (
range that can be NOT NULL,
constraint column_name1
placed in a column FirstName varchar(255),
data_type(size) constraint_name);
Address varchar(255),
City varchar(255)
)

CREATE TABLE table_name
(
column_name1
data_type(size) constraint_name,
Constraints used to specify rules for column_name2
the data in a table. data_type(size) constraint_name,
column_name3
data_type(size) constraint_name,
....
);
Create statement is used to CREATE DATABASE
CREATE DATABASE dbname;
Database create a database. my_db;
CREATE INDEX PIndex
Create Statement used to CREATE UNIQUE INDEX index_name
ON Persons (LastName,
INDEX create indexes table ON table_name (column_name)
FirstName)
CREATE TABLE Persons
CREATE TABLE table_name (
( PersonID int,
Statement is used to column_name1 data_type(size), LastName varchar(255),
Create table create a table in a column_name2 data_type(size), FirstName varchar(255),
database column_name3 data_type(size), Address varchar(255),
.... City varchar(255)
); );

CREATE TABLE Orders


(
O_Id int NOT NULL,
CREATE TABLE table_name
OrderNo int NOT NULL,
Default Used to insert a default (
P_Id int,
constraint value from a column column_name1
OrderDate date DEFAULT
data_type(size) constraint_name);
GETDATE()
)
SQL QUICK REFERENCE BY TABLEAU GURUS
DELETE FROM table_name
WHERE some_column=some_value;
DELETE FROM Customers
WHERE
[or]
used to delete rows in a CustomerName='Alfreds
DELETE
table Futterkiste' AND
DELETE FROM table_name;
ContactName='Maria
or
Anders';
DELETE * FROM table_name;
[used for deleting all rows]
used to return only
SELECT DISTINCT column_name , SELECT DISTINCT City
Distinct distinct (different)
column_name FROM table_name; FROM Customers;
values
DROP
statement is used to
DATABASE DROP DATABASE database_name
delete a database
statement
DROP
INDEX Used to delete an index
DROP INDEX table_name.index_name
Statement in a table

CREATE TABLE Orders


(
Let's illustrate the O_Id int NOT NULL
CREATE TABLE table_name
foreign key with an PRIMARY KEY,
Foreign Key (
example. Look at the OrderNo int NOT NULL,
constraint column_name1
following t P_Id int FOREIGN KEY
data_type(size) constraint_name);
REFERENCES
Persons(P_Id)
)
SELECT
Customers.CustomerName,
Orders.OrderID
SELECT column_name(s)
FROM Customers
Return all rows when FROM table1
FULL OUTER JOIN Orders
Full join there is a match in ONE FULL OUTER JOIN table2
ON
of the tables ON table1.column_name=table2.column_n
Customers.CustomerID=Or
ame;
ders.CustomerID
ORDER BY
Customers.CustomerName;
GroupBy Used in conjuction with SELECT column_name, SELECT
the aggregate to group aggregate_function(column_name) Shippers.ShipperName,CO
the result set by one or FROM table_name UNT(Orders.OrderID) AS
more columns WHERE column_name operator value NumberOfOrders FROM
GROUP BY column_name; Orders
LEFT JOIN Shippers
ON
Orders.ShipperID=Shippers
.ShipperID
SQL QUICK REFERENCE BY TABLEAU GURUS
GROUP BY ShipperName;
SELECT column_name, SELECT Employees.LastName,
aggregate_function(column_name) COUNT(Orders.OrderID) AS
Having Clause was NumberOfOrders FROM (Orders
FROM table_name INNER JOIN Employees
added to SQL beacause
WHERE column_name operator value ON
Having the where keyboard Orders.EmployeeID=Employees.Empl
GROUP BY column_name
could not be used with oyeeID)
HAVING GROUP BY LastName
aggregate function
aggregate_function(column_name) HAVING COUNT(Orders.OrderID) >
10;
operator value;
SELECT column_name(s)
allows you to specify SELECT * FROM Customers
IN Operator FROM table_name
multiple values in a WHERE City IN
WHERE column_name IN
WHERE clause. ('Paris','London');
(value1,value2,...);
txtUserId = getRequestString("UserId");
can destroy your SELECT * FROM Users
INJECTION txtSQL = "SELECT * FROM Users WHERE
database WHERE UserId = 105 or 1=1
UserId = " + txtUserId;
SELECT Orders.OrderID,
Customers.CustomerName,
SELECT <column name> FROM Orders.OrderDate
Returns all rows when
table_name FROM Orders
Inner joins there is at least one
INNER JOIN column_name INNER JOIN Customers
match in BOTH tables
ON condition1=condition2; ON
Orders.CustomerID=Custo
mers.CustomerID;
INSERT INTO Customers
(CustomerName,
INSERT ContactName, Address,
INSERT used to insert new INTO table_name (column1,column2,colu City, PostalCode, Country)
INTO records in a table mn3,...) VALUES ('Cardinal','Tom B.
VALUES (value1,value2,value3,...); Erichsen','Skagen
21','Stavanger','4006','Norwa
y');
statement selects data
from one table and
INSERT INSERT INTO table2 INSERT INTO Customers
inserts it into an
INTO (column_name(s)) (CustomerName, Country)
existing table. Any
SELECT SELECT column_name(s) SELECT SupplierName,
existing rows in the
FROM table1; Country FROM Suppliers;
target table are
unaffected
statement selects data SELECT *
INTO SELECT *
from one table and INTO
Statement INTO newtable [IN externaldb]
inserts it into a new CustomersBackup2013
FROM table1;
table. FROM Customers;
Left join Return all rows from SELECT column_name(s) SELECT
the left table, and the FROM table1 Customers.CustomerName,
matched rows from the LEFT JOIN(or)LEFT OUTER table2 Orders.OrderID
right table ON table1.column_name=table2.column_n FROM Customers
SQL QUICK REFERENCE BY TABLEAU GURUS
LEFT JOIN Orders
ON
ame; Customers.CustomerID=Or
ders.CustomerID
ORDER BY
Customers.CustomerName;
LIKE used to search for a SELECT column_name(s)
SELECT * FROM Customers
 Operator specified pattern in a FROM table_name
WHERE City LIKE 's%';
column. WHERE column_name LIKE pattern;
CREATE TABLE
PersonsNotNull
(
CREATE TABLE table_name P_Id int NOT NULL,
NOT NULL
Enforces a column to ( LastName varchar(255)
CONSTRAI
not accept null column_name1 NOT NULL,
NTS
data_type(size) constraint_name); FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
SELECT column_name, column_name
used to sort the result- FROM table_name SELECT * FROM Customers
OrderBy
set. ORDERBY column_name ASC|DESC, col ORDER BY Country;
umn_name ASC|DESC;
CREATE TABLE Persons
(
P_Id int NOT NULL,
CREATE TABLE table_name LastName varchar(255)
It is uniquely identifies
Primary Key ( NOT NULL,
each record in a
constraint column_name1 FirstName varchar(255),
database table
data_type(size) constraint_name); Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
SELECT Orders.OrderID,
Employees.FirstName
SELECT column_name(s) FROM Orders
Return all rows from
FROM table1 RIGHT JOIN Employees
the right table, and the
Right join RIGHT JOIN/Right outer join table2 ON
matched rows from the
ON table1.column_name=table2.column_n Orders.EmployeeID=Emplo
left table
ame; yees.EmployeeID
ORDER BY
Orders.OrderID;
used to select data from SELECT column_name,column_name SELECT CustomerName,
Select
a database. FROM table_name; City FROM Customers;
SQL Dates Function Description

GETDAT Returns the current date


SQL QUICK REFERENCE BY TABLEAU GURUS
E() and time
DATEPA Returns a single part of a
RT() date/time
DATEA Adds or subtracts a specified
DD() time interval from a date
DATEDI Returns the time between two
FF() dates
CONVE Displays date/time data in
RT() different formats
used to specify the SELECT TOP number|
TOP Clause SELECT TOP 2 * FROM
number of records to percent column_name(s)
Customers;
return. FROM table_name;
To delete only the data
Truncate
inside the table but not TRUNCATE TABLE table_name
table
the table
SELECT City FROM
Customers
combines the result of SELECT column_name(s) FROM table1
UNION UNION/union all
two or more SELECT UNION/union all
operator SELECT City FROM
statements. SELECT column_name(s) FROM table2;
Suppliers
ORDER BY City;
CREATE TABLE Persons
(
P_Id int NOT NULL
CREATE TABLE table_name UNIQUE,
UNIQUE
Identifies each record ( LastName varchar(255)
CONSTRAI
in a database table column_name1 NOT NULL,
NTS
data_type(size) constraint_name); FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
UPDATE Customers
SET ContactName='Alfred
UPDATE table_name
update existing records Schmidt', City='Hamburg'
UPDATE SET column1=value1,column2=value2,...
in a table. WHERE
WHERE some_column=some_value;
CustomerName='Alfreds
Futterkiste';
CREATE VIEW [Products
Above Average Price] AS
SELECT
CREATE VIEW view_name AS
ProductName,UnitPrice
SELECT column_name(s)
Views It is a virtual table FROM Products
FROM table_name
WHERE
WHERE condition
UnitPrice>(SELECT
AVG(UnitPrice) FROM
Products)
Where used to filter records. SELECT column_name,column_name SELECT * FROM Customers
SQL QUICK REFERENCE BY TABLEAU GURUS
FROM table_name
WHERE Country='Mexico';
WHERE column_name operator value;
Wild Description
card
% A substitute for zero or more
characters
_ A substitute for a single character
used to substitute for
[char Sets and ranges of characters to match
Wildcards any other character(s) SELECT * FROM Customers
list]
in a string. WHERE City LIKE 'ber%';
[^cha Matches only a character
rlist] NOT specified
or within 
[!
charl
ist]

SQL Aggregate Functions


AVG() Returns the average value
COUNT() Returns the number of rows
FIRST() Returns the first value
LAST() Returns the last value
MAX() Returns the largest value
MIN() Returns the smallest value
SUM() Returns the sum
SQL SCALAR FUNCTIONS
UCASE() Converts a field to upper case
LCASE() Converts a field to lower case
MID() Extract Characters from a text field
LEN() Returns the length of a text table
ROUND() Rounds a numeric field to the number of decimals
specified
NOW() Returns the current system data and time
FORMAT() Formats how a field is to be displayed.

You might also like