Lectern

Search

Search IconIcon to open search

SQL

Last updated Oct 13, 2022

Abbreviated term for Structured Query Language, often pronounced as ‘sequel’. Used as a language used in databases to query for data according to specified instructions by the language. Used to create, query, and update relational databases.

# Terminologies

TermDefinition
Result setThe produced data after processing a query
ViewA table that is a saved result set
Virtual tableNot a table in the database, but the query code is saved instead
NullA value that is unknown, not available, or not applicable
TupleA term usually synonymous with a record in a table
RelationA term usually synonymous with a table

# Statements and functions

# Statements

StatementDescription
SELECTSelects the given fields from a database
SELECT ... FROM ... WHERESelects the given fields from a database, provided the conditions provided are true
FROMSpecifies which table should data be retrieved from
ASAliasing a field name in the query
DISTINCTFilters a field and removes duplicate values
CREATE VIEWCreates a new view
ORDER BY ... ASC or ORDER BY ... DESCSorts a field in ascending or descending order
LIKEPerforms pattern match searching; often % is used in conjunction to select zero or more characters
IN (...)Checks if the condition matches one of the items in a given list (similar to concatenating multiple ORs)
BETWEEN ... AND ...Checks if the condition is between the two given items; often used with dates
ANDConcatenates conditions together such that both must be true to pass
ORConcatenates conditions together such that at least one must be true to pass
NOTInverses a condition

# Functions

# String functions

FunctionSyntaxKindDescription
LOWERLOWER(str)ScalarConverts a string to lowercase
UPPERUPPER(str)ScalarConverts a string to uppercase
REPLACEREPLACE(str, old_substring, new_substring)ScalarReplaces a string with another given value
STRSTR(num)ScalarConverts a numeral into a string
SUBSTRINGSUBSTRING(str, start, end)ScalarReturns part of a string, inclusive of start and end

# Mathematical functions

FunctionSyntaxKindDescription
CEILINGCEILING(num)ScalarReturns the next integer of a given number
FLOORFLOOR(num)ScalarReturns the previous integer of a given number
ROUNDROUND(num, places)ScalarRounds up a number to a given number of decimal places
COUNTCOUNT(attribute)AggregateReturns the number of items in a group
MINMIN(attribute)AggregateSelects the minimum value in a set of values
MAXMAX(attribute)AggregateSelects the maximum value in a set of values
AVERAGEAVERAGE(attribute)AggregateReturns the average value in a set of values
SUMSUM(attribute)AggregateReturns the sum of all values in a set of values

# datetime functions

Note that for each syntax, interval refers to DAY, MONTH, YEAR, WEEK, HOUR, MINUTE, SECOND, etc.

FunctionSyntaxKindDescription
DATEADDDATEADD(interval, num, date)ScalarAdds a given interval to a date
DATEDIFFDATEDIFF(interval, end, start)ScalarReturns the difference between two dates
GETDATEGETDATE()ScalarGets the current date
DAYDAY(date)ScalarReturns an integer of the day of the date
MONTHMONTH(date)ScalarReturns an integer of the month of the date
YEARYEAR(date)ScalarReturns an integer of the year of the date
FORMATFORMAT(date, format, culture)ScalarReturns a date as a string in the given format

# System functions

FunctionSyntaxKindDescription
FORMATFORMAT(val, format)ScalarReturns a value formatted with the given format
ISNULLISNULL(attribute, replaced_value)ScalarReplaces NULL values with a given value

# Logical operators

OperatorOperation
=equality
!= or <>not
<less than
<=less than or equal to
>greater than
>=greater than or equal to

# Data types

Often data typed and store a particular piece of required information; common data types include:

Data typeUsed for
VARCHARA variable-length string; can be used for anything ranging from an individual character to a string of thousands of characters.
INTEGER or INTA medium-sized integer (32-bits)
BOOLBoolean

# Best practices