Query Keywords Flashcards
ADD
adds a column in an existing table
Example
ALTER TABLE Customers
ADD Email varchar(255)
ADD CONSTRAINT
Adds a constraint after a table is already created
Example
ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
ALTER
adds, deletes or modifies columns in a table,or changes the data type of a column in a table
Example
ALTER TABLE Customers
ADD Email varchar(255)
ALTER COLUMN
changes the data type of a column in a table
Example
ALTER TABLE Employees
ALTER COLUMN BirthDate year;
ALTER TABLE
adds, deletes, or modifies columns in a table
Example
ALTER TABLE Customers
ADD Email varchar(255);
ALL
returns true if all the subquery values meet the condition
Example
SELECT ProductName
FROM Products
WHERE ProductID = ALL (SELECT ProductID FROM OrderDetails WHERE Quantity = 10);
AND
only includes rows where both conditions are true
Example
SELECT * FROM Customers
WHERE Country=’Germany’ AND City=’Berlin’;
ANY
returns true if any of the subquery values meet the condition
Example
SELECT ProductName
FROM Products
WHERE ProductID = ANY (SELECT ProductID FROM OrderDetails WHERE Quantity = 10)
AS
renames a column or table with an alias
Examples
SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;
ASC
sorts the result set in ascending order
Example
SELECT * FROM Customers
ORDER BY CustomerName ASC;
BACKUP DATABASE
creates a back up of an existing database
Example
BACKUP DATABASE testDB
TO DISK = ‘D:\backups\testDB.bak’;
CASE
creates different outputs based on conditions
Example
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN “The quantity is greater than 30”
WHEN Quantity = 30 THEN “The quantity is 30”
ELSE “The quantity is under 30”
END
FROM OrderDetails;
CHECK
a constraint that limits the value that can be placed in a columns
Exmaple
ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName);
COLUMN
changes the data type of a column or deletes a column in a table
Example
ALTER TABLE Employees
ALTER COLUMN BirthDate year;
CREATE
Creates a database, index, view, table or procedure
Example
CREATE DATABASE testDB;
CREATE DATABASE
creates a database
Example
CREATE DATABASE testDB;
CREATE INDEX
creates an index on a table (allows duplicate values)
Example
CREATE INDEX idx_lastname
ON Persons (LastName);
CREATE OR REPLACE VIEW
updates a view Example CREATE OR REPLACE VIEW [Brazil Customers] AS SELECT CustomerName, ContactName, City FROM Customers WHERE Country = "Brazil";
CREATE TABLE
creates a new table in the database Example CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) );
CREATE PROCEDURE
create a stored procedure Example CREATE PROCEDURE SelectAllCustomers AS SELECT * FROM Customers GO;
CREATE UNIQUE INDEX
creates a unique index on a table (no duplicate values)
Example
CREATE UNIQUE INDEX uidx_pid
ON Persons (PersonID);
CREATE VIEW
creates a view based on the result set of a SELECT statement Exmaple CREATE VIEW [Brazil Customers] AS SELECT CustomerName, ContactName FROM Customers WHERE Country = "Brazil";
DATABASE
creates or deletes an SQL database
Example
CREATE DATABASE testDB;
DEFAULT
a constraint that provides a default value for a column Example CREATE TABLE Persons ( City varchar(255) DEFAULT 'Sandnes' );
DELETE
deletes rows from a table
Example
DELETE FROM Customers WHERE CustomerName=’Alfreds Futterkiste’;
DESC
sorts the result set in descending order
Example
SELECT * FROM Customers
ORDER BY CustomerName DESC;
DISTINCT
selects on distinct(different) values in the result set
Exmaple
SELECT DISTINCT Country FROM Customers;
DROP
deletes a column, constraint,database, index, table or view
Example
ALTER TABLE Customers
DROP COLUMN ContactName;
DROP COLUMN
deletes a column in a table
Example
ALTER TABLE Customers
DROP COLUMN ContactName;
DROP CONSTRAINT
deletes a unique, primary key , foreign key or check constraint
Example
ALTER TABLE Persons
DROP CONSTRAINT UC_Person;
DROP DATABASE
delete an existing SQL database
Example
DROP DATABASE testDB;
DROP DEFAULT
deletes a DEFAULT constraint
Example
ALTER TABLE Persons
ALTER COLUMN City DROP DEFAULT;
DROP INDEX
deletes and index in a table
Example
ALTER TABLE table_name
DROP INDEX index_name;
DROP TABLE
deletes an existing table in a database
Example
DROP TABLE Shippers;
DROP VIEW
deletes a view
Example
DROP VIEW [Brazil Customers];
EXEC
executes a stored procedure
Example
EXEC SelectAllCustomers;
EXISTS
Tests for the existence of any record in a subquery
Example
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE SupplierId = Suppliers.supplierId AND Price < 20);
FOREIGN KEY
A constraint that is a key used to link two tables together Example CREATE TABLE Orders ( OrderID int NOT NULL, OrderNumber int NOT NULL, PersonID int, PRIMARY KEY (OrderID), FOREIGN KEY (PersonID) REFERENCES Persons(PersonID) );
FROM
specifies which table to select or delete data from
Example
SELECT CustomerName, City FROM Customers;
FULL OUTER JOIN
returns all rows when there is a match in either left table or right table
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
GROUP BY
groups the result set(used with aggregate functions: COUNT, MAX, MIN, SUM, AVG) Example SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;
HAVING
used instead of WHERE with aggregate functions Example SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;
IN
allows you to specify multiple values in a WHERE clause
EXAMPLE
SELECT * FROM Customers
WHERE Country IN (‘Germany’, ‘France’, ‘UK’);
INDEX
creates or deletes an index in a table
Example
CREATE INDEX idx_lastname
ON Persons (LastName);
INNER JOIN
returns rows that have matching values in both tables
Example
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
INSERT INTO
inserts new rows into a table
Example
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES (‘Cardinal’, ‘Tom B. Erichsen’, ‘Skagen 21’, ‘Stavanger’, ‘4006’, ‘Norway’);
INSERT INTO SELECT
copies data from one table into another table
Example
INSERT INTO Customers (CustomerName, City, Country)
SELECT SupplierName, City, Country FROM Suppliers;
IS NULL
tests for empty values Example SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NULL;
IS NOT NULL
tests for non-empty values Example SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT NULL;
JOIN
joins tables, Same as INNER JOIN (either can be used)
Example
SELECT Orders.OrderID, Customers.CustomerName
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
LEFT JOIN
returns all rows from the left table and the matching rows from the right table
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
LIKE
searches for a specified pattern in a column
Example
SELECT * FROM Customers
WHERE CustomerName LIKE ‘a%’;
LIMIT
specifies the number of records to return in the result set
Example
SELECT * FROM Customers
LIMIT 3;
NOT
only includes rows where a condition is not true
Example
SELECT * FROM Customers
WHERE NOT Country=’Germany’;
NOT NULL
a constraint that enforces a column to not accept NULL values Example CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Age int );
OR
includes rows where either condition is true
Example
SELECT * FROM Customers
WHERE City=’Berlin’ OR City=’München’;
ORDER BY
sorts the result set in ascending or descending order
Example
SELECT * FROM Customers
ORDER BY CustomerName;
OUTER JOIN
returns all rows when there is a match in either left table or right table
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
PRIMARY KEY
a constraint that uniquely identifies each record in a database table Example CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, PRIMARY KEY (ID) );
PROCEDURE
a stored procedure Example CREATE PROCEDURE SelectAllCustomers AS SELECT * FROM Customers GO;
RIGHT JOIN
returns all rows from the right table and the matching rows from the left table
Example
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
ROWNUM
specified the number of records to return in the result set
Example
SELECT * FROM Customers
WHERE ROWNUM <= 3;
SELECT
selects data from the database
Example
SELECT CustomerName, City FROM Customers;
SELECT DISTINCT
Selects only distinct (different) values
Example
SELECT DISTINCT Country FROM Customers;
SELECT INTO
Copies data from one table into a new table
Example
SELECT DISTINCT Country FROM Customers;
SELECT TOP
specifies the number of records to return in the result set
Example
SELECT TOP 3 * FROM Customers;
SET
specifies which column and values that should be updated
Example
UPDATE Customers
SET ContactName = ‘Alfred Schmidt’, City= ‘Frankfurt’
WHERE CustomerID = 1;
TABLE
creates a table, or adds, deletes, or modifies columns in a table, or deletes a table or data inside a table Example CREATE TABLE TestTable AS SELECT customername, contactname FROM customers;
TOP
specifies the number of records to return in the result set
Example
SELECT TOP 3 * FROM Customers;
TRUNCATE TABLE
deletes the data inside a table but not the table itself
Example
TRUNCATE TABLE Categories;
UNION
combines the result set of two or more SELECT statements (only distinct values) Example SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY City;
UNION ALL
combines the result set of two or more SELECT statements (allows duplicate values) Example SELECT City FROM Customers UNION ALL SELECT City FROM Suppliers ORDER BY City;
UNIQUE
a constraint that ensure that all values in a column are unique Example CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, UNIQUE (ID) );
UPDATE
updates existing rows in a table Example UPDATE Customers SET ContactName = 'Alfred Schmidt', City= 'Frankfurt' WHERE CustomerID = 1;
VALUES
specifies the values of an INSERT INTO statement
Example
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES (‘Cardinal’, ‘Tom B. Erichsen’, ‘Skagen 21’, ‘Stavanger’, ‘4006’, ‘Norway’);
VIEW
creates, updates or deletes a view Example CREATE VIEW [Brazil Customers] AS SELECT CustomerName, ContactName FROM Customers WHERE Country = "Brazil";
WHERE
filters a result set to include only records that fulfil a specified condition
Example
SELECT * FROM Customers
WHERE Country=’Mexico’;