Learning SQL Part 7: SQL SELECT Statement — S1
In this lesson we will learn almost all the important phrases in the SELECT sentence.
When you think about learning SQL, one of the first things you come across is the SELECT statement. Selecting information is arguably the most important SQL feature. In this article, we’ll demonstrate the typical use cases for SQL SELECT with practical examples. Read this article if you’re a beginner database user and would like to know what SELECT can do for you.
SQL SELECT is probably the most widely used SQL statement,
SQL SELECT statement is used for fetching some data from a database. The statement is just a query that claims what data to be retrieved, where to search for it, and how to modify it before returning.
In most cases, the SELECT query is run with FROM. SELECT states columns and FROM indicates a table where these columns are located.
SELECT statement options:
SQL SELECT has different clauses to manage the data output. They are: FROM, AS, GROUP BY, HAVING, INTO, ORDER BY, * (asterisk). Let’s see how we can use each clause within the SELECT syntax.
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 SELECT Statement:
The SQL SELECT Statement is used to select data from a database, the data returned is stored in a result table, called the result-set.
Syntax Of SELECT Statement:
The SELECT statement is divided into two
A- SELECT *: The following SQL statement selects all the employees from the “employee” table.
B- SELECT Column Name: The following SQL statement selects the “emp_id” and “first name” and “last name” and employees from the “employee” table.
2- SQL SELECT DISTINCT Statement:
The SELECT DISTINCT statement is used to return only distinct (different) values, next to a table, a column often contains many duplicate values; And sometimes you just want to list the different (distinctive) values. This can be done using the SELECT DISTINCT statement. Different values will be output based on the column you want to extract different values from. If you use one column, the different rows will be output in this column only, and if you use two or more columns, it will be output. The different rows in these columns.
Syntax Of SELECT DISTINCT Statement:
3- SQL WHERE Clause Statement:
The WHERE clause is used to filter rows, It is used to extract only those rows that fulfill a specified condition.
Syntax Of Where Clause:
Note: The
WHERE
clause is not only used inSELECT
statements, it is also used inUPDATE
,DELETE
, etc.!
4- SQL AND, OR and NOT Operators:
The WHERE
clause can be combined with AND
, OR
, and NOT
operators.
The AND
and OR
operators are used to filter records based on more than one condition:
- The
AND
operator displays a record if all the conditions separated byAND
are TRUE. - The
OR
operator displays a record if any of the conditions separated byOR
is TRUE. - The
NOT
operator displays a record if the condition(s) is NOT TRUE. - You can also combine the
AND
,OR
andNOT
operators.
Syntax Of SELECT Statement AND, OR and NOT Operators:
5- SQL ORDER BY Keyword:
The ORDER BY
keyword is used to sort the result-set in ascending or descending order, the ORDER BY
keyword sorts the records in ascending order by default or you can use ASC
for ascending order. To sort the records in descending order, use the DESC
keyword, you can also sort by multiple columns.
Syntax Of ORDER BY Keyword:
6- SQL NULL Values:
What is a NULL Value?
A field with a NULL value is a field with no value, if a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then, the field will be saved with a NULL value, A NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is one that has been left blank during record creation!.
How to Test for NULL Values:
It is not possible to test for NULL values with comparison operators, such as =, <, or <>, We will have to use the IS NULL
and IS NOT NULL
operators instead.
Syntax Of IS NULL and IS NOT NULL:
7- The SQL LIMIT Clause:
- The LIMIT clause is used to specify the number of records to return.
- The LIMIT clause is useful on large tables with thousands of records.
- Returning a large number of records can impact performance.
Syntax Of LIMIT Clause:
8- SQL Some Functions:
- The LIMIT clause is used to specify the number of records to return.
- The MIN() function returns the smallest value of the selected column.
- The MAX() function returns the largest value of the selected column.
- The COUNT() function returns the number of rows that matches a specified criterion.
- The AVG() function returns the average value of a numeric column.
- The SUM() function returns the total sum of a numeric column.
- use SQL Functions: SELECT Function_name(COLUMN_NAME).
- If you want to know more about SQL Functions check this: SQL Functions.
Syntax Of Some SQL Functions Clause:
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.