SQL Tutorial 8: The Select Query


SQL Tutorial 8: The Select Query

The SQL SELECT statement is used to fetch the data from a database table which returns this data in the form of a result table. 

These result tables are called result-sets.

The basic syntax of the SELECT statement is as follows -

SELECT column1, column2, columnN FROM table_name;

SELECT * FROM table_name;

Note: If you want to use/Practice Select statements then we must have at least one table with some records...

Step 1: Create a Database
Create Database gcreddy;

Step 2: Create a Table
Before Create a table first Use/Select database...

Create table abcd(
Id int, 
Name varchar(50), 
Age int, 
City varchar(50),
);  

Step 3: Enter Some Records into that Table then execute select statements...

Insert Into abcd
(ID, Name, Age, City)
values (1, 'Ramesh', 32, 'Pune'),
(2, 'Venkat', 25, 'Chennai'),
(3, 'Suma', 25,'Hyderabad'),
(4, 'Bhasha', 23, 'Delhi'),
(5, 'David', 27, 'Hyderabad');
-------------------
SQL> SELECT Id, Name, City FROM abcd;

It will return all te records of abcd table with ID, Name, and City fields only…

SQL> SELECT * FROM abcd;

It will return all the records of Customers table

SQL –Where Clause 

The SQL WHERE clause is used to specify a condition while fetching the data from a single table or by joining with multiple tables.

The WHERE clause is not only used in the SELECT statement, but it is also used in the UPDATE, DELETE statement, etc.,

Syntax:

SELECT column1, column2, columnN 
FROM table_name
WHERE [condition]

You can specify a condition using the comparison or logical operators like 
>, <, =, LIKE, NOT, etc.

Example:
Select Id, Name 
From abcd
Where Age > 25;

Select ID, Name, Age, City
From abcd
Where Name ='David';

AND and OR Conjunctive Operators:

The SQL AND & OR operators are used to combine multiple conditions to narrow data in an SQL statement.

The AND Operator---

Syntax:

SELECT column1, column2, columnN 
FROM table_name
WHERE [condition1] AND [condition2]...AND [conditionN];

Examples:

Select ID, Name, City
From abcd
Where Id >2 And Age > 25;

Select * From abcd
Where Id>2 And Age>=25;

The OR Operator:

Syntax:

SELECT column1, column2, columnN 
FROM table_name
WHERE [condition1] OR [condition2]...OR [conditionN]

Example:

Select Id, Name, City
From abcd
Where Id>2 Or Age>25;

0 comments:

Post a Comment