SQL

Structured Query Language

SQL Wildcards

The SQL WILDCARDS are used along with the SQL LIKE operator in the SELECT statement. The WILDCARDS are quite complex but are very useful in retrieving data. Consider if there’s no wildcards then we could not search in the database easily. You would have to enter each and every character to match the required result. So, for easier search, SQL WILIDCARDS comes to rescue.

Now what wildcards really are? They are set of characters which lets the DBMS to know that what and how to look for the query.

Some Important Wildcards

SQL TOP Clause

The SQL provides us with the option to retrieve a specific set of data from the table, rather than fetching all the table or only some specific rows by mentioning the matching condition in the WHERE clause.

Now what if you want to get the top 10 rows or top 5 rows from the table, or you simply want to retrieve the last, middle number of rows. But the top clause is not supported by all DBMS, some have respective syntax. However the SQL Limit can also act in the same way. For Oracle equivalent is Rownum.

Syntax

SQL IN Operator

The SQL IN operator is used with the WHERE clause in the SELECT statement, this can be used to look to the dataset matching the condition and looking in the supplied data, in easier words, the IN operator can make further selections in the selected data.

The SQL IN operator can also be used to write nested SQL queries, the nested query first fetches some data and then the wrapper query select or simply filters more from the selected data.

Now let's see the IN operator in Action.

SQL IN Syntax

SQL LIKE Operator

The SQL provides us with the option to retrieve a specific set of data from the table, based on matching it with some sort of string with which we are unsure how it is stored in the database, e.g. if one does not know what is the actual spelling of certain thing, or he simply wants to search the database on the given values, the LIKE operator can be used in building a simple search bar on the website, which will get all the results from the database that matched the string given in the search Bar.

SQL BETWEEN Operator

The SQL BETWEEN operator is used with the WHERE clause in the SELECT statement, this can be used to look for the data between the minimum and the maximum values given to the clause, The BETWEEN clause has DBMS specific behavior, some of the DBMS will do an inclusive between and some will do exclusive. That will also include the values mentioned as the minimum and maximum and some will ignore the minimum and maximum and will simply list the values in between them.

SQL AND / OR Operator

The AND/OR can be used to make a complex or multiple condition for the WHERE clause while selecting, updating or deleting some data.

The AND works as follows

Condition 1 AND condition 2 AND……… condition N

We can use as many conditions as we want with the AND. This condition’s result will only come out to be true if all the conditions are specifies, just like the and truth table

SQL ORDER BY Clause

The ORDER BY clause is used in the SQL SELECT statement to order the data in ascending or descending order, this may help us get a better look when analyzing data, the ORDER BY clause can sort numeric as well as alphabetical data in both ascending and descending order.

SQL ORDER BY Clause Syntax

SELECT column_name(s) FROM table_name ORDER BY column ASC|DESC

ASC as ascending order
DESC as descending order

Example:

SELECT * FROM Dept ORDER BY Sal

OR