Understanding SELECT and FROM Statements in SQL: A Beginner’s Guide
Introduction
In this article we walk through the fundamentals of the SELECT and FROM clauses in SQL, using two sample tables – EmployeeDemographics and EmployeeSalary. The concepts covered include selecting columns, limiting rows, retrieving distinct values, counting records, renaming output columns, and calculating aggregate statistics such as MAX, MIN, and AVG. We also explain how to reference a specific database and table with the FROM clause.
Selecting Data
- Select all columns:
SELECT * FROM EmployeeDemographics; - Select specific columns:
SELECT FirstName, LastName FROM EmployeeDemographics; - Limit rows:
SELECT TOP 5 * FROM EmployeeDemographics;– useful when a table contains millions of rows and you only need a sample.
Distinct Values
SELECT DISTINCT EmployeeID FROM EmployeeDemographics;returns all nine rows because each ID is unique.SELECT DISTINCT Gender FROM EmployeeDemographics;returns only two rows (Male,Female) because those are the unique gender values.
Counting Records
SELECT COUNT(LastName) FROM EmployeeDemographics;counts non‑NULL values in the LastName column. In the example it returns 9.- To give the result a readable column name, use AS:
sql SELECT COUNT(LastName) AS LastNameCount FROM EmployeeDemographics;The output column will be labeled LastNameCount instead of the default "No column name".
Aggregate Functions on Numeric Data
Using the EmployeeSalary table:
- Maximum salary: SELECT MAX(Salary) FROM EmployeeSalary; → $65,000
- Minimum salary: SELECT MIN(Salary) FROM EmployeeSalary; → $36,000
- Average salary: SELECT AVG(Salary) FROM EmployeeSalary; → $48,555
These functions are handy for quick statistical insights.
The FROM Clause and Database Context
- By default the query runs against the database currently selected in the query window (e.g.,
master). If the target table isn’t in that database, an error occurs. - To query a table in a different database, specify the full path:
sql SELECT * FROM SQLTutorialDB.dbo.EmployeeSalary; - SQLTutorialDB – the database name
- dbo – the schema (often
dbofor default) - EmployeeSalary – the table name
- This explicit reference overrides the default database context, allowing you to pull data from any accessible database.
What’s Next?
The next lessons will cover: - WHERE clause – filtering rows based on conditions. - GROUP BY and ORDER BY – organizing and sorting result sets. These complete the core SQL SELECT toolkit before moving on to more advanced topics.
Recap of Key Commands
SELECT *– all columnsSELECT column1, column2– specific columnsSELECT TOP n– limit rowsSELECT DISTINCT column– unique valuesSELECT COUNT(column) AS Alias– count non‑NULL entries with a custom labelSELECT MAX/MIN/AVG(column)– aggregate statisticsFROM Database.Schema.Table– fully qualified table reference
Mastering SELECT and FROM basics—choosing columns, limiting rows, extracting distinct values, counting, and using aggregates—gives you the essential toolkit to query any SQL table efficiently, even across different databases.
Frequently Asked Questions
Who is Alex The Analyst on YouTube?
Alex The Analyst is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.
Does this page include the full transcript of the video?
Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.
What’s Next?
The next lessons will cover: - **WHERE** clause – filtering rows based on conditions. - **GROUP BY** and **ORDER BY** – organizing and sorting result sets. These complete the core SQL SELECT toolkit before moving on to more advanced topics.