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;