• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/31

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

31 Cards in this Set

  • Front
  • Back

Insert into

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



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

Update

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

Like

SELECT * FROM Customers
WHERE Country LIKE '%land%';

Wildcards

%A substitute for zero or more characters


_A substitute for a single character


[charlist]Sets and ranges of characters to match


[^charlist]
or
[!charlist]Matches only a character NOT specified within the bra

Wildcards

SELECT * FROM Customers
WHERE City LIKE '[a-c]%';

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID

he INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns. If there are rows in the "Customers" table that do not have matches in "Orders", these customers will NOT be listed.

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders
ON Customers.CustomerID=Orders.CustomerID

The LEFT JOIN keyword returns all the rows from the left table (Customers), even if there are no matches in the right table (Orders).

SELECT Orders.OrderID, Employees.FirstName
FROM Orders
RIGHT JOIN Employees
ON Orders.EmployeeID=Employees.EmployeeID

The RIGHT JOIN keyword returns all the rows from the right table (Employees), even if there are no matches in the left table (Orders).

Joins

* INNER JOIN: Returns all rows when there is at least one match in BOTH tables
* LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
* RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
* FULL JOIN: Return all rows when there is a match in ONE of the tables

Select into

SELECT *
INTO newtable [IN externaldb]
FROM table1;

Insert into

INSERT INTO table2
SELECT * FROM table1;

Constraints

* NOT NULL - Indicates that a column cannot store NULL value
* UNIQUE - Ensures that each row for a column must have a unique value
* PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have an unique identity which helps to find a particular record in a table more easily and quickly
* FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table
* CHECK - Ensures that the value in a column meets a specific condition
* DEFAULT - Specifies a default value when specified none for this column

Index

The CREATE INDEX statement is used to create indexes in tables.


Indexes allow the database application to find data fast; without reading the whole table.

IsNull

ISNULL(UnitsOnOrder,0)

Sql 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 field
* ROUND() - Rounds a numeric field to the number of decimals specified
* NOW() - Returns the current system date and time
* FORMAT() - Formats how a field is to be displayed

The HAVING Clause

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

Union vs Union All

UNION removes duplicate records (where all columns in the results are the same), UNION ALL does not.

Pivot

SELECT ,


[first pivoted column] AS ,


[last pivoted column] AS


FROM


PIVOT


(


()


FOR


[]


IN ( [first pivoted column], ... [last pivoted column])


) AS


;

Dynamic Sql

DECLARE @city varchar(75)
SET @city = 'London'
SELECT * FROM customers WHERE City = @city

Dynamic Sql EXEC

DECLARE @sqlCommand varchar(1000)
DECLARE @columnList varchar(75)
DECLARE @city varchar(75)
SET @columnList = 'CustomerID, ContactName, City'
SET @city = '''London'''
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM customers WHERE City = ' + @city
EXEC (@sqlCommand)

Dynamic Sql sp_executesql

DECLARE @sqlCommand nvarchar(1000)
DECLARE @columnList varchar(75)
DECLARE @city varchar(75)
SET @columnList = 'CustomerID, ContactName, City'
SET @city = 'London'
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM customers WHERE City = @city'
EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75)', @city = @city

Clustered Index vs Non Clustered Inde

Clustered Index


* Only one per table
* Faster to read than non clustered as data is physically stored in index order
* Can be used many times per table
* Quicker for insert and update operations than a clustered index


Both types of index will improve performance when select data with fields that use the index but will slow down update and insert operations.


Because of the slower insert and update clustered indexes should be set on a field that is normally incremental ie Id or Timestamp.


SQL Server will normally only use an index if its selectivity is above 95%.

Index

An index is used to speed up the performance of queries. It does this by reducing the number of database data pages that have to be visited/scanned.

Join

Given the following tables, representing pilots that can fly planes and planes in a hangar:



create table PilotSkills ( pilot_name char(15) not null, plane_name char(15) not null )



create table Hangar ( plane_name char(15) not null )


Select the names of the pilots who can fly every plane in the hangar.


select distinct pilot_name from PilotSkills as ps1 where not exists


( select * from hangar where not exists ( select * from PilotSkills as ps2 where ps1.pilot_name = ps2.pilot_name and ps2.plane_name = hangar.plane_name ) )

in vs exist vs join

SELECT * FROM tableA WHERE tableA.id IN (SELECT tableB.id FROM tableB WHERE title = 'Analyst');


in vs exist vs join

SELECT * FROM tableA


WHERE EXISTS


(SELECT 1 FROM tableB


WHERE title = 'Analyst' AND tableA.id = tableB.id);

in vs exist vs join

SELECT * FROM tableA


JOIN tableB ON tableA.id = tableB.id


WHERE tableB.title = 'Analyst';

in vs exist vs join

In most cases, EXISTS or JOIN will be much more efficient (and faster) than an IN statement.


in vs exist vs join

When using an IN combined with a subquery, the database must process the entire subquery first, then process the overall query as a whole, matching up based on the relationship specified for the IN.


With an EXISTS or a JOIN, the database will return true/false while checking the relationship specified. Unless the table in the subquery is very small, EXISTS or JOIN will perform much better than IN.