SQL Notes
SQL Notes
This will create an unique index for the column Email in the table Customers. This index, along with speeding up
queries like a normal index, will also force every email address in that column to be unique. If a row is inserted or
updated with a non-unique Email value, the insertion or update will, by default, fail.
This creates an index on Customers which also creates a table constraint that the EmployeeID must be unique.
(This will fail if the column is not currently unique - in this case, if there are employees who share an ID.)
This creates an index that is sorted in descending order. By default, indexes (in MSSQL server, at least) are
ascending, but that can be changed.
By default rebuilding index is offline operation which locks the table and prevents DML against it , but many RDBM
allow online rebuilding. Also, some DB vendors offer alternatives to index rebuilding such as
REORGANIZE
(SQLServer) orCOALESCE/ SHRINK SPACE(Oracle).
This will fail if an unique index is set on the Email column of Customers. However, alternate behavior can be defin
for this case:
SELECT
ROW_NUMBER() OVER(ORDER BY Fname ASC) AS RowNumber,
Fname,
LName
FROM Employees
SELECT
ROW_NUMBER() OVER(PARTITION BY DepartmentId ORDER BY DepartmentId ASC) AS RowNumber,
DepartmentId, Fname, LName
FROM Employees
SELECT
storeName,
COUNT(*) AS total_nr_orders,
COUNT(DISTINCT userId) AS nr_unique_customers,
AVG(orderValue) AS average_order_value,
MIN(orderDate) AS first_order,
MAX(orderDate) AS lastOrder
FROM
orders
GROUP BY
storeName;
While DISTINCT is used to list a unique combination of distinct values for the specified columns.
SELECT DISTINCT
storeName,
userId
FROM
orders;
storeName userId
Store A 43
Store B 57
Store C 82
Store A 21
This example uses a Common Table Expression and a Window Function to show all duplicate rows (on a subset of
columns) side by side.
Using string functions, you can, for example, combine data, extract a substring, compare strings, or convert a stri
to all uppercase or lowercase characters.
Some databases (e.g., Oracle) perform implicit lossless conversions. For example, a on a CLOBand NCLOB
CONCAT
yields aNCLOB. A CONCATon a number and avarchar2 results in avarchar2 , etc.:
DECLARE @str varchar(100) = 'Hello ' --varchar is usually an ASCII string, occupying 1 byte per
char
SELECT DATALENGTH(@str) -- returns 6
DECLARE @nstr nvarchar(100) = 'Hello ' --nvarchar is a unicode string, occupying 2 bytes per char
SELECT DATALENGTH(@nstr) -- returns 12
Oracle
Examples:
SELECT value FROM STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ');
Result:
value
-----
Lorem
ipsum
dolor
sit
Example:
SELECT REPLACE( 'Peter Steve Tom', 'Steve', 'Billy' ) --Return Values: Peter Billy Tom
Syntax:
Example:
Oracle SQL doesn't have LEFT and RIGHT functions. They can be emulated with SUBSTR and LENGTH.
SUBSTR ( string-expression, 1, integer )
SUBSTR ( string-expression, length(string-expression)-integer+1, integer)
FirstName Address
James South New York
John South Boston
Michael South San Diego
Select Statement :
SELECT
FirstName,
REPLACE (Address, 'South', 'Southern') Address
FROM Employees
ORDER BY FirstName
Result:
Update Statement :
We can use a replace function to make permanent changes in our table through following approach.
Update Employees
Set city = (Address, 'South', 'Southern');
A more common approach is to use this in conjunction with a WHERE clause like this:
Update Employees
Set Address = (Address, 'South', 'Southern')
Where Address LIKE 'South%';
PARSENAME function returns the specific part of given string(object name). object name may contains string like
object name,owner name, database name and server name.
Syntax
PARSENAME('NameOfStringToParse',PartIndex)
Example
PARSENAME will returns null is specified part is not present in given object name string
Result:
Result:
List Concatenation aggregates a column or expression by combining the values into a single string for each group
string to delimit each value (either blank or a comma when omitted) and the order of the values in the result can
specified. While it is not part of the SQL standard, every major relational database vendor supports it in their own
way.
MySQL
SELECT ColumnA
, GROUP_CONCAT(ColumnB ORDER BY ColumnB SEPARATOR ',') AS ColumnBs
FROM TableName
GROUP BY ColumnA
ORDER BY ColumnA;
PostgreSQL
SELECT ColumnA
, STRING_AGG(ColumnB, ',' ORDER BY ColumnB) AS ColumnBs
FROM TableName
GROUP BY ColumnA
ORDER BY ColumnA;
SQL Server
SQL Server 2016 and earlier
WITH CTE_TableName AS (
SELECT ColumnA, ColumnB
FROM TableName)
SELECT t0.ColumnA
, STUFF((
SELECT ',' + t1.ColumnB
FROM CTE_TableName t1
WHERE t1.ColumnA = t0.ColumnA
ORDER BY t1.ColumnB
FOR XML PATH('')), 1, 1, '') AS ColumnBs
FROM CTE_TableName t0
GROUP BY t0.ColumnA
ORDER BY ColumnA;
SQLite
without ordering:
SELECT ColumnA
, GROUP_CONCAT(ColumnB, ',') AS ColumnBs
FROM TableName
GROUP BY ColumnA
ORDER BY ColumnA;
WITH CTE_TableName AS (
SELECT ColumnA, ColumnB
FROM TableName
ORDER BY ColumnA, ColumnB)
SELECT ColumnA
, GROUP_CONCAT(ColumnB, ',') AS ColumnBs
FROM CTE_TableName
GROUP BY ColumnA
ORDER BY ColumnA;
EXAMPLE TABLE
city_name population year
New York City 8,550,405 2015
New York City ... ...
New York City 8,000,906 2005
To select the average population of the New York City, USA from a table containing city names, population
measurements, and measurement years for last ten years:
QUERY
select city_name, AVG(population) avg_population
from city_population
where city_name = 'NEW YORK CITY';
Notice how measurement year is absent from the query since population is being averaged over time.
RESULTS
city_name avg_population
New York City 8,250,754
Note: The AVG() function will convert values to numeric types. This is especially important to keep in mind
when working with dates.
You can count over a column/expression with the effect that will not count thevalues:
NULL
You can also use DISTINCT inside of another function such as COUNT to only find the DISTINCT members of the
set to perform the operation on.
For example:
Will return different values. The SingleCount will only Count individual Continents once, while the AllCount will
include duplicates.
ContinentCode
OC
EU
AS
NA
NA
AF
AF
AllCount: 7 SingleCount: 5
Syntax:
Syntax:
You use scalar functions wherever an expression is allowed within a T-SQL statement.
The DATENAMEfunction returns the name or value of a specific part of the date.
You use theGETDATEfunction to determine the current date and time of the computer running the current SQL
instance. This function doesn't include the time zone difference.
In the syntax, datepart is the parameter that specifies which part of the date you want to use to calculate
difference. The datepart can be year, month, week, day, hour, minute, second, or millisecond. You then specify th
start date in the startdate parameter and the end date in the enddate parameter for which you want to find the
difference.
The lower(char) function converts the given character parameter to be lower-cased characters.
would return the customer's last name changed from "SMITH" to "smith".
In SQL, most data conversions occur implicitly, without any user intervention.
To perform any conversions that can't be completed implicitly, you can use theor CONVERTfunctions.
CAST
The CAST function always uses the default style setting. For example, it will represent dates and times using the
format YYYY-MM-DD.
The CONVERTfunction uses the date and time style you specify. In this case, 3 specifies the date format dd/mm/yy.
USE AdventureWorks2012
GO
SELECT FirstName + ' ' + LastName + ' was hired on ' +
CAST(HireDate AS varchar(20)) AS 'Cast',
FirstName + ' ' + LastName + ' was hired on ' +
CONVERT(varchar, HireDate, 3) AS 'Convert'
FROM Person.Person AS p
JOIN HumanResources.Employee AS e
ON p.BusinessEntityID = e.BusinessEntityID
GO
Cast Convert
David Hamiltion was hired on 2003-02-04 David Hamiltion was hired on 04/02/03
In the syntax for the function, you specify the string that must be converted,
AS the
keyword, and then the required
If the string value can't be converted to a numeric, date, or time format, it will result in an error. You'll then need
use CAST or CONVERTfor the conversion.
The CHOOSEfunction returns an item from a list of values, based on its position in the list. This position is specified
by the index.
In the syntax, the index parameter specifies the item and is a whole number, or integer. The val_1 … val_n
parameter identifies the list of values.
The IIF function returns one of two values, based on a particular condition. If the condition is true, it will return
true value. Otherwise it will return a false value.
In the syntax, the boolean_expression parameter specifies the Boolean expression. The true_value parameter
specifies the value that should be returned if the boolean_expression evaluates to true and the false_value
parameter specifies the value that should be returned if the boolean_expression evaluates to false.
In this example, you use the IIF function to return one of two values. If a sales person's year-to-date sales are abo
200,000, this person will be eligible for a bonus. Values below 200,000 mean that employees don't qualify for
bonuses.
SQL includes several mathematical functions that you can use to perform calculations on input value
return numeric results.
One example is theSIGN function, which returns a value indicating the sign of an expression. The value of -1
indicates a negative expression, the value of +1 indicates a positive expression, and 0 indicates zero.
In the example, the input is a negative number, so the Results pane lists the result -1.
In the syntax, the float_expression parameter specifies the expression, and the y parameter specifies the power t
which you want to raise the expression.
You use a scalar expression to specify the values that should be compared. The offset parameter is the number o
rows before the current row that will be used in the comparison. If you don't specify the number of rows, the
default value of one row is used.
The default parameter specifies the value that should be returned when the expression at offsetNULL
hasvalue.
a If
you don't specify a value, a valueNULL
of is returned.
The LEAD function provides data on rows after the current row in the row set. For example, in a statement,
SELECT
you can compare values in the current row with values in the following row.
You specify the values that should be compared using a scalar expression. The offset parameter is the number of
rows after the current row that will be used in the comparison.
You specify the value that should be returned when the expression at offset NULL
has avalue using the default
parameter. If you don't specify these parameters, the default of one row is used and a value
NULLof
is returned.
This example uses the LEAD and LAG functions to compare the sales values for each employee to date with those
the employees listed above and below, with records ordered based on the BusinessEntityID column.
To find the exact value from the row that matches or exceeds the 0.5 percentile, you pass the percentile as the
numeric literal in the
PERCENTILE_DISC function. The Percentile Discreet column in a result set lists the value of the
row at which the cumulative distribution is higher than the specified percentile.
The first value in the result set always has a percent rank of zero. The value for the highest-ranked – or last – valu
in the set is always one.
The CUME_DISTfunction calculates the relative position of a specified value in a group of values, by determining th
percentage of values less than or equal to that value. This is called the cumulative distribution.
The PERCENT_RANKfunction ranks the entries within each group. For each entry, it returns the percentage of entries
in the same group that have lower values.
The CUME_DISTfunction is similar, except that it returns the percentage of values less than or equal to the current
value.