SQL: How to Use Like & Order By Clause in SQL

The LIKE clause in SQL is used to filter results based on a pattern or substring match. It is particularly useful when searching for specific patterns within text or string data. The LIKE clause uses the percent sign (%) as a wildcard to match any character or sequence of characters. This allows for flexible and powerful pattern matching capabilities.

The ORDER BY clause is used to sort the result set in ascending or descending order based on one or more columns. It is particularly useful when analyzing large amounts of data and allows for easy identification of trends and patterns. The ORDER BY clause can be used with any SELECT statement and allows for sorting based on a single column or multiple columns. It is important to note that the ORDER BY clause should be used with caution, as sorting large datasets can be resource-intensive and may impact performance.

LIKE Clause

The LIKE clause in SQL is used in the WHERE clause of a SELECT, UPDATE or DELETE statement to filter results based on a pattern match. The basic syntax for using the LIKE clause is:

SELECT column1, column2, …
FROM table_name
WHERE column_name LIKE pattern;

For example, to select all columns and rows from the “employees” table where the “last_name” starts with ‘Smi’:

SQL
SELECT * FROM employees
WHERE last_name LIKE 'Smi%';

The % is used as a wildcard operator to match any string of any length (including zero length)

For example, to select all columns and rows from the “employees” table where the “first_name” ends with ‘son’:

SQL
SELECT * FROM employees
WHERE first_name LIKE '%son';

The _ is used as a wildcard operator to match a single character.
For example, to select all columns and rows from the “employees” table where the “first_name” has ‘a’ as the second character:

SQL
SELECT * FROM employees
WHERE first_name LIKE '_a%';

The SQL implementation you are using for specific information on the syntax and options that are available for using the LIKE clause in conjunction with the WHERE clause.

TOP, LIMIT, or ROWNUM Clause

In SQL, The TOP, LIMIT, or ROWNUM clause is used to limit the number of rows returned by a SELECT statement. The syntax for using these clauses varies depending on the SQL implementation you are using.

In Microsoft SQL Server and Sybase SQL, the TOP clause is used to limit the number of rows returned by a SELECT statement. The basic syntax is:

SELECT TOP number column1, column2, …
FROM table_name;

For example, to select the top 10 rows from the “employees” table:

SQL
SELECT TOP 10 * FROM employees;

In MySQL and PostgreSQL, the LIMIT clause is used to limit the number of rows returned by a SELECT statement. The basic syntax is:

SELECT column1, column2, …
FROM table_name
LIMIT number;

For example, to select the top 10 rows from the “employees” table:

SQL
SELECT * FROM employees
LIMIT 10;

In Oracle, the ROWNUM clause is used to limit the number of rows returned by a SELECT statement. The basic syntax is:

SELECT column1, column2, …
FROM table_name
WHERE ROWNUM <= number;

For example, to select the top 10 rows from the “employees” table:

SQL
SELECT * FROM employees
WHERE ROWNUM <= 10;

The SQL implementation you are using for specific information on the syntax and options that are available for using the TOP, LIMIT, or ROWNUM clauses in conjunction with the SELECT statement. You can check the official documentation for each of them.

ORDER BY Clause

The ORDER BY clause in SQL is used to sort the results of a SELECT, UPDATE, or DELETE statement based on one or more columns. The basic syntax for using the ORDER BY clause is:-

SELECT column1, column2, …
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], …;

For example, select all columns and rows from the “employees” table and sort the results by the “last_name” in ascending order:

SQL
SELECT * FROM employees
ORDER BY last_name;

The default sorting order is ascending (ASC), but you can also sort the results in descending order (DESC) by specifying the keyword after the column name.

SQL
SELECT * FROM employees
ORDER BY last_name DESC;

You can also sort the results by multiple columns. For example, select all columns and rows from the “employees” table and sort the results first by the “last_name” in ascending order and then by the “first_name” in descending order:

SQL
SELECT * FROM employees
ORDER BY last_name, first_name DESC;

You can also use expressions or functions in the ORDER BY clause. For example, select all columns and rows from the “employees” table and sort the results by the length of “last_name” in descending order:

SQL
SELECT * FROM employees
ORDER BY LENGTH(last_name) DESC;

The SQL ORDER BY clause is used to sort the data in ascending or descending order, based on one or more columns. Some databases sort the query results in ascending order by default.

In conclusion, the LIKE, WHERE, and ORDER BY clauses are all important components of SQL queries that allow for filtering, pattern matching, and sorting of data. These clauses provide powerful capabilities for querying and analyzing data in a flexible and efficient manner. However, it is important to use these clauses with caution and ensure that they are used in a way that does not impact the performance of the database. By understanding and using these SQL clauses effectively, users can extract valuable insights from their data and make informed decisions based on their analysis.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.