Saturday, 25 January 2020

Query data using select command in multiple ways using T-SQL

Hi All,

Hope you all are doing good.

In this article, I will explain you about how to use select command in different scenarios.
To retrieve data for specific columns, use below statement
Syntax:SELECT column1, column2, ..., columnn FROM table_name;
Example:SELECT first_name, last_name FROM Customers;

To retrieve data for all columns, use below statement
Syntax:SELECT * FROM table_name;
Example:SELECT * FROM Customers;

Select statement with WHERE clause
Syntax:SELECT * FROM table_name where condition;
Example:SELECT * FROM Customers where state='KA';

Select statement with ORDER BY clause
Syntax:
SELECT column_list  FROM table_name
ORDER BY [column_name | expression] [ASC | DESC ]
Example:SELECT * FROM Customers ORDER BY first_name DESC;

Select statement with GROUP BY clause
Syntax:SELECT col1, col2, ..., AggregateFun(coln) FROM table_name GROUP BY column_name;
Example:SELECT city, COUNT (*) FROM Customers GROUP BY city;

Select statement with TOP
Syntax:SELECT TOP (expression) [PERCENT] [WITH TIES] FROM table_name
Example:SELECT TOP 10 product_name, list_price FROM Products
Example:SELECT TOP 1 PERCENT product_name, list_price FROM Products
Example:SELECT TOP 3 WITH TIES product_name, list_price FROM Products

Select statement with DISTINCT
Syntax:SELECT DISTINCT
 column_name1,
 column_name2,
 ...
FROM table_name
Example:SELECT DISTINCT city FROM Customers

Select Into:
The SELECT INTO statement creates a new table and inserts rows from the query into it.

Syntax:SELECT columns
INTO new_table
FROM source_table
[WHERE condition]
Example:SELECT * INTO MarketingCustomers FROM SalesCustomers;

DISTINCT, WHERE, AND, OR, IN, BETWEEN operators
OperationExample
DISTINCTSELECT DISTINCT city, state, zip_code FROM Customers
WHERESELECT * FROM Customers where state='KA';
ANDSELECT * FROM Customers where state='KA' AND city='BLR';
ORSELECT * FROM Customers where state='KA' OR state='AP';
INSELECT * FROM Customers where state IN ('KA','AP','TS');
NOT INSELECT * FROM Customers where state NOT IN ('TN','KL');
BETWEENSELECT * FROM Customers where ID BETWEEN 1 AND 10;

Thanks for reading my article.

No comments:

Post a Comment

Intoduction to Flutter

Hi All, I hope every one is doing good In this article, we will know about the introduction of Flutter.