Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Use the SELECT Command in SQL Server 2017 (Part 1)

DZone's Guide to

How to Use the SELECT Command in SQL Server 2017 (Part 1)

The SELECT command in SQL Server has a ton of potential, especially when it comes to filtering out certain data. Come check out exactly how it works!

· Database Zone
Free Resource

Navigating today's database scaling options can be a nightmare. Explore the compromises involved in both traditional and new architectures.

In this article, you will learn how to select data using the basic SELECT statement, filter data with the WHERE clause, and filter data with the WHERE clause using various operators.

The SELECT statement is used to select data from a table. The data returned is stored in a result table called the result set.

Basic SELECT Statement

Syntax:

SELECT column1, column2, ...columnN FROM table_name; 

Here, column1column2, etc. are the field names of the table you want to select data from.

Example:

SELECT CustomerId,CustomerName,Country,City,Age,Address FROM Customers; 

If you want to select all the fields available in the table, use the following syntax:

Syntax:

SELECT * FROM table_name;

Example:

SELECT * FROM Customers; 

Note: For better performance, use column names instead of *.

Filtering Data With the WHERE Clause

The WHERE clause is used to filter records. It will extract only those records that fulfill a specified condition.

Syntax:

SELECT column1, column2, ...columnN FROM table_name WHERE condition; 

Example:

SELECT CustomerId,CustomerName,Country,City,Age,Address FROM Customers WHERE CustomerId=1; 

Note: The WHERE clause is not only used in a SELECT statement; it is also used in UPDATE and DELETE statements.

Filtering Data With the WHERE Clause Using Various Operators

Let's look at not equal operators, greater than operators, and less than operators.

Not Equal Operator

When you require getting all the records from a table excluding a specific value, you can use the Not Equal Operator.

Example:

SELECT CustomerId,CustomerName,Country,City,Age,Address FROM Customers WHERE Country<>'India'; 

The above query will fetch all the records from the Customers table, excluding the records that have the country as 'India'.  

Greater Than Operator

SELECT CustomerId,CustomerName,Country,City,Age,Address FROM Customers WHERE Age>30; 

Less Than Operator

SELECT CustomerId,CustomerName,Country,City,Age,Address FROM Customers WHERE Age<30; 

Still want more clarification? Check out the video below!


Resources

Understand your options for deploying a database across multiple data centers - without the headache.

Topics:
sql server 2017 ,ms sql ,database ,tutorial ,select ,filtering data

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}