Saturday, February 25, 2023

SQL Server - SELECT Statement, Working with TABLES

 


SQL SELECT Statement

The SELECT statement is used to fetch/extract data from a database.

Syntax :

SELECT Field1, Field2, ...
FROM table_name;

Example :

SELECT EmpIDName, ...
FROM Employee;

 

SQL SELECT DISTINCT Statement

SELECT DISTINCT statement is used to fetch/return only different field values.

Inside a table, a column often contains many duplicate values. DISTINCT allows you list the different values records only.

 

Syntax :

SELECT DISTINCT Field1, Field2, ...
FROM table_name;

Example :

SELECT DISTINCT EmpId, Name, ...
FROM Employee;

 

SQL SELECT WHERE Clause

SELECT WHERE clause is used to fetch/return selective or filtered records.

Syntax :

SELECT Field1, Field2, ...
FROM table_name

WHERE Field1 >=Value;

Example :

SELECT EmpId, Name,
FROM Employee

WHERE EmpId >= 100;

WHERE clause can be combined with AND, OR, and NOT operators

SELECT EmpId, Name,
FROM Employee

WHERE EmpId >= 100 and city = ‘Delhi’;

SELECT EmpId, Name,
FROM Employee

WHERE EmpId >= 100 OR city = ‘Delhi’;

ORDER BY keyword

SELECT EmpId, Name,
FROM Employee

WHERE EmpId >= 100 OR city = ‘Delhi’

ORDER BY EmplD ASC, City DESC;

 

DELETE STATEMENT

DELETE statement is used to delete records from table

DELETE FROM Employee where EmpId= 101;

 

SELECT TOP Statement

SELECT TOP clause is used to fetch/return number of records from the top of the table.

Syntax :

SELECT TOP 10 Field1, Field2, ...
FROM table_name

WHERE Field1 >=Value;

Example :

SELECT TOP 10 EmpId, Name,
FROM Employee

WHERE EmpId >= 100;

 

SELECT LIKE Statement

The LIKE operator is used in a WHERE clause to search for a specific records in a column.

There are two wildcards used with the LIKE operator

·         The percent sign (%) represents zero, one, or multiple characters

·         The underscore sign (_) represents one, single character

 

Below statement fetch all records with name starts with character ‘d’

SELECT *
FROM Employee

WHERE Name Like ‘d%’;

Below statement fetch all records with name ends with character ‘d’

SELECT *
FROM Employee

WHERE Name Like ‘%d’;

Below statement fetch all records with name having character ‘d’ in 2nd position

SELECT *
FROM Employee

WHERE Name Like ‘_d%’;

No comments:

Post a Comment

SQL Server - Introduction to SQL

  Introduction to SQL   SQL is a standard language for accessing and manipulating databases. SQL stands for Structured Query Language. SQL...