PRAKTIKUM BASIS DATA
DATA MANIPULATION LANGUAGE (DML)
The SQL SELECT TOP Clause
The SELECT TOP clause is useful on large tables with thousands of
records. Returning a large number of records can impact on
performance.
SELECT TOP number|percent
column_name(s)
FROM table_name
WHERE condition;
SELECT TOP 3 * FROM Customers;
SELECT TOP 50 PERCENT * FROM Customers;
The SQL MIN() and MAX() Functions
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
SELECT MIN(column_name)
FROM table_name
WHERE condition;
SELECT MAX(column_name)
FROM table_name
WHERE condition;
The SQL COUNT(), AVG() and SUM()
Functions
The COUNT() function returns the number of rows that matches a
specified criteria.
The AVG() function returns the average value of a numeric
column.
The SUM() function returns the total sum of a numeric column.
SELECT COUNT | AVG | SUM (column_name)
FROM table_name
WHERE condition;
The SQL GROUP BY Statement
The GROUP BY statement is often used with aggregate functions
(COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or
more columns.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;
The SQL HAVING Clause
The HAVING clause was added to SQL because the WHERE
keyword could not be used with aggregate functions.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
THE END