0% found this document useful (0 votes)
23 views11 pages

Introduction To Database Systems: Lecture #5 Ketevan Grigalashvili

Lecture #5 introduces key SQL concepts including the SELECT TOP clause for retrieving limited records, the SELECT DISTINCT statement for fetching unique values, and various built-in functions for mathematical, string, and date manipulations. It covers specific functions such as ROUND, LEN, and DATEADD, detailing their syntax and usage. Additionally, it discusses functions for handling NULL values and converting data types.

Uploaded by

zluka6930
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views11 pages

Introduction To Database Systems: Lecture #5 Ketevan Grigalashvili

Lecture #5 introduces key SQL concepts including the SELECT TOP clause for retrieving limited records, the SELECT DISTINCT statement for fetching unique values, and various built-in functions for mathematical, string, and date manipulations. It covers specific functions such as ROUND, LEN, and DATEADD, detailing their syntax and usage. Additionally, it discusses functions for handling NULL values and converting data types.

Uploaded by

zluka6930
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

INTRODUCTION TO DATABASE Lecture #5

SYSTEMS Ketevan Grigalashvili


RETRIEVING TOP VALUES
The SELECT TOP clause is used to specify the number of records to
return.
The SELECT TOP clause is useful on large tables with thousands of
records. Returning a large number of records can impact
performance.
We can also ask to fetch the percentage of all of the rows.
SELECT TOP 10 (PERCENT) column1, column2, …
FROM table_name
DATA SELECTION OF UNIQUE VALUES
(DISTINCT)
The SELECT DISTINCT statement is used to return only distinct
(different) values. It removes duplicated values.
You can apply the DISTINCT keyword to a single column, multiple
columns, or an entire table. When used on multiple columns or the
entire table, it returns unique combinations of values across the
specified fields.
SELECT DISTINCT column1, column2, …
FROM table_name
SQL BUILT-IN FUNCTIONS
A built-in function in SQL is a pre-defined function provided by the
database system that performs a specific task, such as
mathematical operations, string manipulations, date calculations, or
aggregate computations.
MATHEMATICAL FUNCTIONS
Mathematical functions in SQL are primarily used with numeric values. Here are the math functions used
frequently:

Syntax Description

ROUND(number, decimals[, operation]) The ROUND() function rounds a number to a specified number
of decimal places.
CEILING(number) The CEILING() function returns the smallest integer value that
is larger than or equal to a number.
FLOOR(number) The FLOOR() function returns the largest integer value that is
smaller than or equal to a number.
ABS(number) The ABS() function returns the absolute value of a number.
STRING FUNCTIONS
String functions in SQL are used to manipulate and process text (string) values.
Syntax Description

LEN(string) The LEN() function returns the length of a string.


Note: Trailing spaces at the end of the string is not
included when calculating the length.
LEFT(string, number_of_chars) The LEFT() function extracts a number of characters
from a string (starting from left).
RIGHT(string, number_of_chars) The RIGHT() function extracts a number of
characters from a string (starting from right).
SUBSTRING(string, start, length) The SUBSTRING() function extracts some characters
from a string.
CHARINDEX(substring, string, start) The CHARINDEX() function searches for a substring
in a string, and returns the position.
STRING FUNCTIONS
Syntax Description

LTRIM(string) The LTRIM() function removes leading spaces from a


string.
RTRIM(string) The RTRIM() function removes trailing spaces from a
string.
TRIM([characters FROM ]string) The TRIM() function removes the space character
OR other specified characters from the start or end
of a string. By default, the TRIM() function removes
leading and trailing spaces from a string.
CONCAT(string1, string2, ...., string_n) The CONCAT() function adds two or more strings
together.
CONCAT_WS(separator, string1, string2, ...., The CONCAT_WS() function adds two or more
string_n) strings together with a separator.
STRING FUNCTIONS
Syntax Description

string1 + string2 + string_n The + operator allows you to add two or more
strings together.
UPPER(string) The UPPER() function converts a string to upper-
case.
LOWER(string) The LOWER() function converts a string to lower-
case.
REPLACE(string, old_string, new_string) The REPLACE() function replaces all occurrences of
a substring within a string, with a new substring.
REVERSE(string) The REVERSE() function reverses a string and returns
the result.
DATE AND TIME FUNCTIONS
SQL Date and Time functions are used to manipulate, extract, calculate, and format date/time
values efficiently.
Syntax Description
DATEADD(interval, number, date) The DATEADD() function adds a time/date interval
to a date and then returns the date.
DATEDIFF(interval, date1, date2) The DATEDIFF() function returns the difference
between two dates, as an integer.
GETDATE() The GETDATE() function returns the current
database system date and time.
DAY(date) The DAY() function returns the day of the month
(from 1 to 31) for a specified date.
MONTH(date) The MONTH() function returns the month part for a
specified date (a number from 1 to 12).
DATE AND TIME FUNCTIONS
Syntax Description

YEAR(date) The YEAR() function returns the year part for a


specified date.

ISDATE(expression) The ISDATE() function checks an expression and


returns 1 if it is a valid date, otherwise 0.

DATEPART(interval, date) The DATEPART() function returns a specified part of


a date.

EOMONTH ( start_date [ , month_to_add ] ) This function returns the last day of the month
containing a specified date, with an optional
offset.
OTHER FUNCTIONS
We also have other useful functions that help us retrieve system data, convert data types, and handle
NULL values:

Syntax Description

ISNULL(expression, value) The ISNULL() function returns a specified value if


the expression is NULL. If the expression is NOT
NULL, this function returns the expression.
COALESCE(val1, val2, ...., val_n) The COALESCE() function returns the first non-null
value in a list.
CAST(expression AS datatype(length)) The CAST() function converts a value (of any type)
into a specified datatype.
SYSTEM_USER The SYSTEM_USER function returns the login name
for the current user.

You might also like