Basic SQL Select + From Statements with Examples
Tags: sql, database, programming, beginners
Published: 2023-08-16 05:56:53

## Basic SQL Select + From Statements with Examples
Within SQL, the SELECT statement serves the purpose of retrieving data from a database. Occasionally, we have specific preferences for how the data should be presented when retrieved from the database, like obtaining the highest number, lowest number, and other variations. This write-up includes SELECT + statements for the following clauses and functions:

## Table of Content
- SELECT + TOP
- SELECT + DISTINCT
- SELECT + COUNT
- SELECT + AS
- SELECT + MAX
- SELECT + MIN
- SELECT + AVG

The table named "EmployeDemographics" presented below will be the focus of our examination. I'm utilizing the MSSQL database management system for this example. Additionally, it's important to note that the SQL functions are not affected by letter cases when typed, and these queries might differ on other databases.


![Table ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qc36dt1amuu5gx846oev.png)


### SELECT + TOP
This is employed to indicate the number of records that should be retrieved from the table.

Example:

```sql
SELECT TOP 5
FROM EmployeDemographics
```

This will select the top 5 data values in every column.


![Top 5 on the table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjqocoxkbni3wcoesosh.png)

The provided illustration gives you the first five data from the complete table, with the option to indicate the specific column you desire to retrieve.

For Example:

```sql
SELECT TOP 5(GENDER)
FROM EmployeDemographics
```

This will return the top 5 data values on the GENDER column.

![Top 5 gender](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42244me3uwhlaz3bg583.png)


### SELECT + DISTINCT
The SELECT DISTINCT statement is utilized to retrieve distinct or unique values exclusively.

For Example:

```sql
SELECT DISTINCT (GENDER)
FROM EmployeDemographics
```

The function used retrieves unique values from the gender column. The outcome shows that there are only two distinct genders in the returned table.

The result:
![distinct Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aouamoc8gvh90xyq5wop.png)

Another Example:

```sql
SELECT DISTINCT (EmplyoeeID)
FROM EmployeDemographics
```

The function mentioned will provide the unique value within the EmployeeID Column.

![distinct employeeId](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqtbh9tq57w4n0q234ug.png)


### SELECT + COUNT
The COUNT() function provides the number of rows that meet a particular condition.

For Example:

```sql
SELECT COUNT(FirstName)
FROM EmployeDemographics
```

The function calculates the count of non-null values for the FirstName column, resulting in a value of 9. Also, if there's a NULL value in the column, it's treated as a count of zero.


![count exercise](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0nczsynpd38hrhpbrsz.png)


### SELECT + AS
This statement or function assigns a column alias, providing an alternate name that can be employed in the resulting set.

In the previous example, you might have observed that the outcome displayed was 9, yet the label assigned to the result set above the column was noted as **(No Column name)**. However, this can be modified.
Let us use another column.

For Example:

```sql
SELECT COUNT (EmplyoeeID) AS LastName
FROM EmployeDemographics
```

You have the flexibility to choose any name that suits your result set. The function above calculates the count of EmployeeID, which is 9, and labels it as LastName. This is an exploration of how the SELECT AS function can be beneficial.

The result:

![alias LastName](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4vfxlbpbnvm8kamzf8v4.png)


Prior to progressing to the MIN, MAX, AND AVG functions, we'll utilize the following table known as 'EMPLOYEESALARY'.


![Employeesalary Table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gdkwjm8pwrbzecn03frs.png). 


### SELECT + MIN
The MIN() function provides the minimum value from the chosen column.

For Example:

```sql
SELECT MIN(Salary)
FROM EmployeeSalary
```

Observing the column, we can see that the lowest value is 20000, and this particular minimum value is anticipated to be the outcome when returned.

![minimum salary](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3czt4ou9p29ldapd3ler.png)


### SELECT + MAX
The max() function returns the maximum value from the chosen column.

For Example:

```sql
SELECT MAX(Salary)
FROM EmployeeSalary
```

As anticipated, the function will provide the maximum value present in the column.

![Max Table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s902f4uza21ibs6jqbe4.png)


### SELECT + AVG
This returns the average of all the values of the Selected numeric column.

For Example:

```sql
SELECT AVG(Salary)
FROM EmployeeSalary
```

The function mentioned above will calculate and yield the average value from our EmployeeSalary Table.


![Average table](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rfqyztbq7gyaacr9n035.png)


### Conclusion
This article serves as a valuable educational tool for individuals who are in the process of learning SQL by offering clear explanations, illustrative visuals, and practical examples for understanding the fundamental SQL select commands.

1. **SELECT + TOP**: Retrieving a specified number of records from a table.

2. **SELECT + DISTINCT**: Retrieving unique values from a column.

3. **SELECT + COUNT**: Counting the number of rows meeting a specific condition.

4. **SELECT + AS**: Assigning column aliases for custom result column names.

5. **SELECT + MIN**: Finding the minimum value in a selected column.

6. **SELECT + MAX**: Finding the maximum value in a selected column.

7. **SELECT + AVG**: Calculating the average of values in a selected numeric column.

These concepts are illustrated using the "EmployeeDemographics" and "EmployeeSalary" tables.

For more SQL articles let's connect on [X](https://twitter.com/vilimzy) and [LinkedIn] (https://www.linkedin.com/in/ibrahim-enemona-abdulrasheed-375807249/)