Experiment No.
-3
Objective:- Restricting and Sorting Data.
Restricting Data
1. Restricting Data using WHERE clause: Restricting data means limiting the rows returned from a
query based on certain conditions. In MySQL, you use the WHERE clause to filter records. The
WHERE clause can include comparison operators, logical operators, and pattern matching to restrict the
data returned by the query.
Syntax: SELECT * or column1, column2, ...
FROM table_name
WHERE condition;
Sorting Data
1. Sorting in Ascending Order: By default, MySQL sorts results in ascending order when the ORDER BY
clause is used. You can explicitly use the ASC keyword, but it is optional.
Syntax: SELECT * or column1, column2, ...
FROM table_name
ORDER BY column1 ASC; or ORDER BY column1;
2. Sorting in Ascending Order: To sort the data in descending order, you can use the DESC keyword
after the column name.
Syntax: SELECT * or column1, column2, ...
FROM table_name
ORDER BY column1 DESC;
3. Sorting by Multiple Columns: You can sort data by multiple columns by listing them in the ORDER
BY clause. MySQL first sorts by the first column. If there are duplicate values in the first column, it sorts
by the second column, and so on.
Syntax: SELECT * or column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;