SQL Básico en Ciencia de Datos
SQL Básico en Ciencia de Datos
1 Order By
2 Limit
3 Aliases
4 Aggregate functions
M IN C MAX I COUNTS I SUM C I Ava C 1
ROUND I
5
String manipulation functions
LENGTH 7 UPPERC I LOWER I 7 SUBSTRINGS
CON CAT I
6 DATE functions
NOW CI CORDATE I CURT INEC DATE
7 NULL functions
IS NULL C IS NOT NULL C COALESCE C
8 NUMBER functions
AB SC CE ICC I FLOOR C I ROUND C J M DC
syntax
SELECT Column 1 column 2
SELECT
FROM Customers
ORDER BY Country City
R
SELECT
FROM Customers
ORDER BY Country ASC City DESC
SQL LIMIT Clause
Syntax
SELECT Column 1 Column 2
LIMIT number
Example
SELECT
FROM Customers
LIMIT 3
Cor
SELECT
FROM customers
LIMIT 3
in MS ACCESS ORACLE
Equivalent Examples
MS ACCESS Example
SELECT TOP 3
FROM Customers
ORACLE Example
SELECT
FROM Customers
WHERE country India AND ROWNUM E3
SQL Aliases
more readable
query
Alias column syntax
SELECT Column name As alias name
Examples
SELECT ID AS Customer ID
FROM customers
FROM customers
er
SELECT ID AS Customer ID
FROM customers
C Customer JD O Customer JD
MI NC syntax
SELECT MIN Column name
WHERE Condition
MAX C syntax
SELECT MAX Column name
WHERE Condition
Examples
SELECT MIN Case As min age
From customers
From customers
SQL Count C Ava C and SUM C Functions
of a numerical column
Count C I syntax
SELECT Count Column name
WHERE condition
AVG I syntax
SELECT AVG Column name
WHERE condition
SUM I syntax
SELECT SUM Column name
WHERE condition
Examples
SELECT COUNT JD
FROM Customers
WHERE JD 2
FROM Customers
FROM Customers
WHERE country India
SQL ROUND C Function
Syntax
SELECT ROUND Column name decimals
FROM table name
Example
SELECT Product Name
Syntax
SELECT LENGTH Column name
Example
SELECT Customer name
FROM customers
SQL LOWER I UPPER I functions
The LOWER C function converts a string
to lowercase
The UPPER I function converts a string
to uppercase
LOWER Syntax
SELECT LOWER Column name
UPPER Suntan
Example
SELECT UPPER Last Name AS Upper lastname
LOWER First Name As lower first name
FROM customers
SQL SUBSTRING C MIDC l functions
from a chara
field ters
tent
SUBSTRING I syntax
SELECT SUBSTRING Column name
MIDC syntax
SELECT MID Column name
Example
SELECT SUBSTRING City 1,4 AS Short city
FROM customers
LOCATE C I Syntan
SELECT LOCATE Substring
column name start As some name
start
parameter is optional default is 1
As position
FROM Customers
start position
y optional
SELECT LOCATE son full name
As position
FROM Customers
REPLACE I syntax
SELECT REPLACE Column name
Example
SELECT REPLACE description old new
FROM products
UPDATE Products
SET description REPLACE description
old new
TRIM I I syntax
SELECT TRIM Character From column name
As some name
Example
SELECT TRIM FROM product name
As trimmed name
FROM Products
FROM products
SQL CONCATC function
The con cat I function is used to
two concate
different ate
tent fields columns in
to one tent field
Syntax
SELECT CON CAT Column I Seperator
columns As some name
Example
As full name
FROM Customers
SQL Dates
The most difficult part when working with
the database
1 DATE YY YY MM DD
2 TIME HH MI Ss
3 DATE TIME YY YY MM DD HH MI SS
y TIMESTAMP YY YY MM DD HH MI SS
5 YEAR YYYY or YY
MYSQL NOW CI Function
The now I returns the current date and
time
syntax
NOW C
output
current DATE TIME
YYYY MM DD HH MI SS
CURDATE C Function
The CURDATEC I returns the current date
Syntax
CURDATEC I
output
current date
YYYY MM DD
CURTIME C Function
The Curti MEC t returns the current time
syntax
CURTI MEC I
output
current time
HH MI SS
DATE C function
The DATEC I extracts the date part of
functions
Syntax
DATE date
YEARC function
The YEAR C I extracts the year part of
functions
Syntax
YEAR date
MONTH C function
The MONTH C I extracts the month part of
functions
Syntax
MONTH date
functions
Syntax
DAY date
DD
usage
SELECT NOW CI AS Current dateline
CURDATEC AS Current date
Syntax
EXTRACT unit FROM date expression
Example
SELECT EXTRACT YEAR FROM 2023 06 03
As extracted Year
As extracted month
As extracted day
we have many supported units in Extracts I
function
MYSQL DATE ADD C I function
date
Syntax
DATE ADD date expression INTERVAL Valve unit
Example
SELECT DATE ADD 2023 06 03 INTERVAL 1
date
Syntax
DATE SUB date expression INTERVAL Valve unit
Example
SELECT DATE SUB 2023 06 03 INTERVAL 1
syntax
DATE DIFF end date start date
As date difference
output
output is 7
Cos
SELECT DATED FF end date start date
As date difference
FROM sales
new column is created with difference in
the dates
MYSQL DATE FORMAT function
The DATE FORM ATC I function is used to format
a date or dateline value in to a specific format
It allows us to convert a date or dateline value
to a
formatted string representation
syntax
DATE FORMAT date value format string
date value is the date or dateline value
tp AM pm indicator eg AM or pm
Example
SELECT DATE FORMAT 2023 06 03 15 30 45
output
03 06 2023 3 30PM
er
SELECT DATE FORMAT date Column f Y Y.m Id
As formatted date
FROM sales
MYSQL NULL Functions
There are several functions available to handle
NULL Values Here are some commonly used
NULL functions
IS NULL checks if a valve is NULL
usage
SELECT column name
Example
SELECT Customer name
FROM Customers
usage
SELECT column name
Example
SELECT Product name
FROM products
WHERE ISNT NULL
quantity
As result
return NULL
Example
SELECT employee name COALESCE salary o
As adjusted salary
From employees
This query returns the employee names and
their adjusted salaries If the salary is NULL
it is replaced with o
IF NULL Takes two expressions returns first
expression if not null or returns second one
usage
SELECT IF NULL expression 1 expression 2
As result
FROM table name
Example
SELECT IF NULL Salary 10000
As adjusted salary
FROM employees
In this example if salary column is
not NULL it will be returned as
usage
SELECT NULLIF expression 1 expression 2
As result
FROM table name
Example
SELECT NULL IF 5 5 As result
be NULL
As adjusted salary
FROM Employees
there if the original value of salary was 5000
usage
SELECT ABS C 10
output 10
usage
SELECT CEIL 4.2
output 5
SELECT CEIL Salary As rounded salary
FROM employees
usage
SELECT FLOOR Y z
output y
er
SELECT FLOOR Salary As rounded Salay
FROM employees
usage
SELECT ROUND Y 567 2
output 4.56
division operation
usage
SELECT MOD 10,3
output I
FROM employees
of another number
usage
SELECT POWER 2,3
output 8
usage
SELECT SORT IG
Output 4
RAND I function Generates a random number
between o and 1
usage
SELECT RAND C
Output 0.846758567301456