Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • SQL Server - Get Started
  • Install SQL Server
  • SQL Server Management Studio
  • SQL Server - Windows Authentication
  • SQL Server - Authentication
  • SQL Server - Create New User
  • SQL Server - GRANT/REVOKE Permissions to User
  • SQL Server - Data Types
  • SQL Server - Naming Conventions
  • SQL Server - CREATE Database
  • SQL Server - CREATE Table
  • Add Columns
  • Identity Column
  • Rename Column, Table
  • Drop Columns
  • SQL Server - Schema
  • SQL Server - Tables Relations
  • SQL Server - Primary Keys
  • Modify/Delete Primary Keys
  • SQL Server - Foreign Keys
  • Modify/Delete Foreign Keys
  • SQL Server - Check Constraints
  • SQL Server - Unique Constraints
  • SQL Server - Views
  • Modify/Delete Views
  • SQL Server - Functions
  • SQL Server - Stored Procedures
  • Stored Procedure Parameters
  • SQL Server - Indexes
  • Non-clustered Indexes
  • Modify/Delete Indexes
  • SQL Server - Triggers
  • DDL Triggers
  • LOGON Triggers
  • Enable/Disable Triggers
  • Modify/Delete Triggers
  • SQL Server - Sequence
  • SQL Server - Synonyms
  • SQL Server - IF ELSE Statement
  • SQL Server - Loops
  • SQL Server - Insert Data
  • SQL Server - Update Data
  • SQL Server - Delete Data
  • SQL Server - Select Query
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • SQL Server - Inner Join
  • Left Join
  • Right Join
  • Full Join
  • Self Join
  • Dynamic SQL
  • Built-in Functions
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

SQL Server - SELECT Statement

In SQL Server, the SELECT statement is used to retrieve rows/columns data from one or more existing tables. It follows the SQL (Structured Query Language) standards.

Syntax:

SELECT column1, column2,...columnN FROM table_name

Select All Columns

The * operator represents all the columns of a table. So, you don't need to specify each column name in the SELECT query to get data from all the columns.

SQL Script: Select Query
SELECT * FROM Employee;

The above query returns all the rows and columns data from the Employee table, as shown below.

Selecting All Records

Select Specific Columns Data

Specify the column names in the SELECT statement to get the data only from those columns, as shown below.

SQL Script: Select Query
SELECT EmpId, FirstName, LastName FROM Employee;

The above query will display the following result.

Filter columns in the select query

Alias for Columns and Table

You can specify an alias for one or more columns in the SELECT query. An alias is a temporary name to a table or column in the query.

Advantage of Alias:

  • Alias makes a column more readable in the result set.
  • Alias is used to give a small, abbreviated, and meaningful name to tables in the query so that it will be easy to refer tables in joining multiple tables.
  • Alias helps us to identify which column belongs to which table in case of getting data from multiple tables.

The following query specifies the "Employee Id" for EmpId, and Name as an alias for the FirstName column in the SELECT query. Specify an alias in the single or double quotes if you want a space or other string in it.

SQL Script: Column Alias
SELECT EmpId "Employee Id", FirstName AS Name FROM Employee;

The above query will display the following result.

+ Operator in SELECT Statement

The + operator in MS SQL Server concatenates string values or adds numeric values. The following concatenates two varchar columns in the result.

SQL Script: + in Select Query
SELECT EmpId, FirstName + ' ' + LastName AS "Full Name" FROM Employee;

The above query will display the following result.

+ operator in select query

The following just sums up numbers in the select query.

SQL Script: + Operator
SELECT 10 + 15; --returns 25 SELECT 10.5 + 15; --returns 25.5

FROM Clause

The SELECT statement must have the FROM clause. The FROM clause is used to list down table names from which we want to select data and specify joins between those tables.

You can specify multiple tables in the FROM clause to select data from. However, if tables have the same columns, then you need to specify full column names like table_name.column_name in the SELECT query.

The following selects columns from two tables.

SQL Script: Select Data From Multiple Tables
SELECT * FROM Employee, Deparatment; SELECT Employee.*, Department.* FROM Employee, Deparatment; SELECT emp.*, dept.* FROM Employee emp, Deparatment dept; SELECT emp.FirstName, dept.DipartmentName FROM Employee emp, Deparatment dept;

Having multiple tables in the FROM clause without the WHERE clause and JOIN would return repeated data from each table. For example, if the Employee table has two rows and the Department table has two rows, then the above query would return four rows where columns of one table would repeat for the columns of another table. You will learn more about it in the JOIN tutorial.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.