msbacademy.org
SQL
Group By, Order By & Aliases
msbacademy.org
Group By
The SQL GROUP BY clause is used in collaboration with
the SELECT statement to arrange identical data into groups. This
GROUP BY clause follows the WHERE clause in a SELECT
statement and precedes the ORDER BY clause
msbacademy.org
Syntax
SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
ORDER BY column1, column2;
Ex: SELECT emp_name,salary
FROM employee
WHERE salary>20000
GROUP BY emp_name
ORDER BY emp_id;
msbacademy.org
Order By Clause
Order By Clause is used to arrange the data either in
ascending or descending order. By default order by clause
arrange or sort the data in ascending order. To sort the records in
a descending order, you can use the DESC keyword.
Note: You can apply for an order by clause on more than one
column in the same table
msbacademy.org
Syntax
SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC,
column_name ASC|DESC;
Ex: SELECT * FROM student ORDER BY branch;
msbacademy.org
Alias
You can rename a table or a column temporarily by giving
another name known as Alias.
msbacademy.org
Syntax
For table:
SELECT column1, column2....
FROM table_name AS alias_name
WHERE [condition];
For column:
SELECT column_name AS alias_name
FROM table_name
WHERE [condition];
msbacademy.org
Alias Example
For table: SELECT e.emp_id, e.emp_name, o.amount
FROM employee AS e, orders AS o
WHERE e.emp_id= o.emp_id;
For column:
SELECT student_id AS id,
student_name AS name
FROM student;
msbacademy.org
Follow Us:
/msbacademy
/msb academy
/msb_academy
/msb_academy
Thank You

Group By, Order By, and Aliases in SQL