SQL Server > Table- Create, Read, Update, Delete (CRUD)

Conditional select statement in SQL Server

How to filter records from the database table using where clause in SQL Server?


Open up a New Query window by clicking on the “New Query” icon from the top-left.

This opens up a new query window in the right panel. Writing Select statement as it is shown in above picture and clicking on “! Execute” icon throws error as shown above because the “MyDetails”

Table doesn’t exists into the database selected. Note that currently the database selected for this query window is “master”.

To select our database, we need to click on the database dropdown as shown below and click on our database name.

Now click on “! Execute” and it will give desired result.

Filtering records based on AutoId

SELECT AutoId, FullName, City FROM MyDetails
  WHERE AutoId > 5

Here, we are instructing SQL Server that give us only those records from the MyDetails table whose AutoId value is more than 5 (condition).

Filtering records based on FullName

Similarly, we can also have the Where clause checking for the FullName column

SELECT AutoId, FullName, City FROM MyDetails
  WHERE FullName = 'Gita'

Here we are filtering records where FullName value is ‘Gita’

Filtering records based on more than one columns - AND

We can also filter records based on multiple columns with AND condition

SELECT AutoId, FullName, City FROM MyDetails
  WHERE FullName = 'Gita' AND AutoId = 6

In this case, we will get only those records from MyDetails table where FullName is ‘Gita’ AND AutoId
is ‘6’.

Filtering records based on more than one columns - OR

We can also filter records based on multiple columns with OR condition

SELECT AutoId, FullName, City FROM MyDetails
  WHERE FullName = 'Gita' OR AutoId = 7

In this case, we will get all records from MyDetails table whose FullName is ‘Gita’ OR AutoId is ’7’, so it is showing both records as both conditions are match in our database table..

 Views: 14143 | Post Order: 34



Write for us






Hosting Recommendations