Learning SQL Part 8: SQL SELECT Statement And More Query — S2

Mohammed || ブラックハート
7 min readAug 12, 2022

In this lesson we will learn almost all the important Keyword in the SELECT sentence and more query.

Oh Noooooooooo

SQL is incredibly powerful, and like every well-made development tool, it has a few commands which it’s vital for a good developer to know. Here is a Some of SQL queries that are really important for coding & optimization. Each of the queries in the SQL tutorial is consequential to almost every system that interacts with an SQL database.

We will use this database schema from Mike Dane Check the link if you want to know how to create a database like this This schema is very ideal for learning SELECT statements.

1- SQL LIKE Operator:

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used in conjunction with the LIKE operator:

  • The percent sign (%) represents zero, one, or multiple characters.
  • The underscore sign (_) represents one, single character.

Some information:

  • You can also combine any number of conditions using AND or OR operators.
  • Typically, you’ll use the LIKE operator in the WHERE clause of the SELECT , DELETE, and UPDATE statement.

Syntax Of LIKE Operator:

Syntax Of LIKE Operator

SQL LIKE Operator Examples:

Syntax Of SELECT Statement

2- SQL IN Operator:

The IN operator allows you to specify multiple values in a WHERE clause.

Some information:

  • The IN operator is a shorthand for multiple OR conditions.
  • MySQL IN Operator used to retrieve the result set by checking whether a value from the set of literal values or provided by the subquery is matched or not.
  • Basically, this operator helps to determine the specific value if available in the result of subquery using the WHERE clause.
  • It fetches the set of columns from the table if a particular value is present in the list of values provided to match a conditional expression.
  • Thus, the MySQL IN operator like other MySQL operators allows comparing a value matching from the set of column values returned by a subquery statement.
  • If we want to get the rows as result from the Database table where a particular value should be present in a list of values available through conditional query then, we use MySQL IN operator.
  • We extract the rows which have matching values in the list of values in IN operator function from a particular value of column given using WHERE keyword in MySQL.

SQL IN Syntax:

IN Syntax

SQL IN Operator Examples:

IN Operator Examples

3- SQL BETWEEN Operator:

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

In SQL we use different types of languages for different functionalities that carry different commands. So mostly while fetching records using Data query language, we use SELECT command. BETWEEN operators comes to picture when we want records/select records within the range of values. These values can either be numbers, date or in string format. Between operator is inclusive i.e. both start and end values are included in the result. In the same manner, we can also use NOT BETWEEN, which will fetch the records outside of the given range of values. In this topic, we are going to learn about MySQL BETWEEN.

SQL BETWEEN Operator Syntax:

SQL BETWEEN Operator Syntax

SQL BETWEEN Operator Examples:

SQL BETWEEN Operator Examples

3- SQL Aliases:

SQL aliases are used to give a table, or a column in a table, a temporary name, MySQL Alias is used when we are willing to provide an alternate name for the column or table. Temporarily assigning a name for column or table that is user friendly and understandable. Alias is mainly used for the column and table. Column Alias are used to give alternate name for the column headings which are easy for the end- user to understand. Table Alias is to provide an alternate table for the tables, which is used to make easier to read and use at the time of joins, aliases are often used to make column names more readable, an alias only exists for the duration of that query, It requires double quotation marks or square brackets if the alias name contains spaces, an alias is created with the AS keyword.

Aliases can be useful when:

  • There are more than one table involved in a query
  • Functions are used in the query
  • Column names are big or not very readable
  • Two or more columns are combined together

Alias Column Syntax:

Alias Column Syntax

Alias Table Syntax:

Alias Table Syntax

Alias multiple columns Syntax:

Alias multiple columns Syntax

SQL Aliases Examples:

SQL Aliases Examples

4- SQL UNION Operator:

The UNION operator is used to combine the result-set of two or more SELECT statements.

  • Every SELECT statement within UNION must have the same number of columns.
  • The columns must also have similar data types.
  • The columns in every SELECT statement must also be in the same order.
  • The column names in the result-set are usually equal to the column names in the first SELECT statement.

SQL UNION Operator is divided into two:

The UNION operator specifies only distinct values by default. The UNION ALL operator defines duplicate values, to allow duplicate values use UNION ALL.

SQL UNION Operator Syntax:

SQL UNION Operator Syntax

SQL UNION Operator Examples:

SQL UNION Operator Examples

5- SQL GROUP BY Statement:

The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country".

The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.

SQL GROUP BY Statement Syntax:

SQL GROUP BY Statement Syntax

SQL GROUP BY Statement Examples:

SQL GROUP BY Statement Examples

6- SQL HAVING Clause:

In real-time applications, we often feel a necessity to generate complex reports from the data that is saved in the database and retrieve the filtered records to display the report to the user. For this, your SQL query must be optimized and correct so that even if a large amount of data is present in the tables the performance of the application is not hampered. The SELECT query that is constructed for such reports needs to make complete use of the functionalities provided in MySQL.

HAVING a clause is one such functionality that helps in applying the filters on the expressions. These expressions can be single or multiple columns or even the condition on the grouped aggregated data that is retrieved using the GROUP BY clause, The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.

SQL HAVING Clause Syntax:

SQL HAVING Clause Syntax

SQL HAVING Clause Examples:

SQL HAVING Clause Examples

7- SQL EXISTS Operator:

The EXISTS operator is used to test for the existence of any record in a subquery, the EXISTS operator returns TRUE if the subquery returns one or more records.

SQL EXISTS is used to find out whether a particular row is existing in the table or not. SQL EXISTS is used with the subquery and returns the rows that are equal or matches to the result returned by the subquery. The statement returns true if the row exists in the table else false. The True is represented by 1 and false is represented by 0. It is very inefficient to use the EXISTS in the SQL since EXISTS re-run for every row in the query table. So, it is significant to not us the EXISTS condition.

SQL EXISTS Operator Syntax:

SQL EXISTS Operator Syntax

SQL EXISTS Operator Examples:

SQL EXISTS Operator Examples

Note: If there is anything wrong with the article, please let me know in the comments.

check this: My Repository All things need to Know About SQL: Learning SQL.

--

--

Mohammed || ブラックハート

Software Engineer: Backend Django, data analysis, ML Engineer in future, Self-Education, Passionate about learning and ML and cleaning and analyzing data