0% found this document useful (0 votes)
15 views

SQL Keywords

Uploaded by

desirecutepol
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

SQL Keywords

Uploaded by

desirecutepol
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

In database management systems (DBMS), SQL is used for querying and managing databases.

Here’s a
breakdown of the key SQL keywords AS, LIMIT, TOTAL, AVG, DISTINCT and GROUP BY along with
examples to illustrate their usage.

1. AS
Purpose: AS is used to create an alias for a column or table. This alias makes complex queries more
readable and can also be used to rename columns in the result set.
Syntax:
SELECT column_name AS alias_name FROM table_name;
Example:
SELECT employee_name AS Name, employee_salary AS Salary
FROM employees;
Explanation: In this example, employee_name is aliased as Name, and employee_salary as Salary,
making the output more readable.
Output:
Name | Salary
------------|--------
John Doe | 50000
Jane Smith | 60000

2. LIMIT
Purpose: LIMIT is used to specify the number of records to return in the result set. It’s often used when
dealing with large datasets to restrict the number of rows displayed.
Syntax:
SELECT column_name FROM table_name LIMIT number_of_rows;
Example:
SELECT employee_name FROM employees LIMIT 3;
Explanation: This query returns only the first three rows from the employees table.
Output:
employee_name
-------------
John Doe
Jane Smith
Peter Parker

3. TOTAL
Purpose: TOTAL is not a standard SQL function but may be used in certain DBMS like SQLite to sum
values. It’s similar to SUM, but TOTAL returns 0.0 instead of NULL when there are no matching rows.
Syntax:
SELECT TOTAL(column_name) FROM table_name;
Example:
SELECT TOTAL(employee_salary) AS TotalSalary FROM employees;
Explanation: This query calculates the total salary of all employees in the employees table. If no salaries
are present, it returns 0.0.
Output:
markdown
TotalSalary
-----------
170000.0

4. AVG
Purpose: AVG is an aggregate function that calculates the average value of a numeric column.
Syntax:
SELECT AVG(column_name) FROM table_name;
Example:
SELECT AVG(employee_salary) AS AverageSalary FROM employees;
Explanation: This query calculates the average salary of all employees in the employees table.
Output:
AverageSalary
-------------
56666.67

5. DISTINCT
Purpose: DISTINCT is used to return unique values, eliminating duplicates from the result set.
Syntax:
SELECT DISTINCT column_name FROM table_name;
Example:
SELECT DISTINCT department FROM employees;
Explanation: This query returns a list of unique departments in the employees table.
Output:
department
----------
IT
HR
Marketing

Combined Example:
Let’s combine some of the above keywords in a single query to illustrate their use together.
 Scenario: You want to find the average salary of employees in the "IT" department and limit the
result to the top 2 rows, displaying the department as "Department" and the average salary as
"Average Salary".
SELECT DISTINCT department AS Department, AVG(employee_salary) AS "Average Salary"
FROM employees
WHERE department = 'IT'
LIMIT 2;

Explanation:
DISTINCT ensures that only unique department names are retrieved.
AS creates aliases for the columns.
AVG calculates the average salary for employees in the IT department.
LIMIT restricts the result to 2 rows.

Output:
Department | Average Salary
-----------|----------------
IT | 60000

Key Takeaways:
 AS helps create aliases for better readability of results.
 LIMIT is useful when working with large datasets to limit the output.
 TOTAL (in SQLite) is used to sum values, returning 0.0 if no rows match.
 AVG calculates the average of a numeric column.
 DISTINCT is essential for eliminating duplicates from result sets.
The GROUP BY clause in SQL is used to group rows that have the same values in specified columns into
aggregated data. It’s often used in conjunction with aggregate functions like COUNT, SUM, AVG, MIN,
or MAX to perform calculations on each group of rows.
Syntax
SELECT column1, aggregate_function(column2)
FROM table_name
WHERE condition
GROUP BY column1;

column1: The column by which the rows will be grouped.


aggregate_function(column2): An aggregate function applied to column2 for each group created
by column1.
Example Scenario
Scenario Description:
Consider a table named orders with the following columns: order_id, customer_id, product, quantity, and
price. You want to calculate the total quantity ordered by each customer.
Table Data:

order_id customer_id product quantity price

1 101 Widget A 3 10.0

2 102 Widget B 2 15.0

3 101 Widget A 1 10.0

4 103 Widget C 4 20.0

5 102 Widget A 2 10.0

Query:
SELECT customer_id, SUM(quantity) AS TotalQuantity
FROM orders
GROUP BY customer_id;
Explanation:
GROUP BY customer_id groups the rows by customer_id.
SUM(quantity) calculates the total quantity for each customer_id.
AS TotalQuantity renames the result column for readability.
Output:

customer_id TotalQuantity

101 4

102 4

103 4

Key Points:
The GROUP BY clause is especially useful for summarizing data and creating reports based on
grouped records.
Columns specified in SELECT but not in aggregate functions must be included in the GROUP
BY clause to avoid errors.
In this example, GROUP BY allowed us to find the total quantity of products ordered by each customer.

You might also like