The SELECT statement is used to retrieve data from a database. Example:
Select * From Customer
Select Name, City from Customer
Select distinct City from customer
SQL WHERE Clause: The WHERE clause is used to extract only those records that fulfill a specified criterion.
It is used to filter records.
Select * from customer where City = 'Delhi' //for text use single quotes
Select * from customer where id = 1
Operators used in WHERE Clause:
= , <> , > , < , >= , <= , Between , Like , In , And , Or , Not
Select * from customer where City = 'Ahmedabad' and Name = 'Ramesh'
Select * from customer where City = 'Bhopal' or city = 'Mumbai'
Select * from customer where Name='Harry' and (City='Delhi' or City='Bhopal')
The BETWEEN operator is used to select values within a range.
Select * from customer where Postal_Code between 110000 and 300000
Select * from customer where Postal_Code Not between 110000 and 300000
Select * from customer where name between 'c' and 'm'
SQL Wildcard Characters: In SQL, wildcard characters are used with the SQL LIKE operator. SQL wildcards are used to search for data within a table.
% - A substitute for zero or more characters
_ - A substitute for a single character
[charlist] - Ranges of characters to match
[^charlist] - Matches only a character NOT specified within the brackets
The LIKE operator is used to search for a specified pattern in a column.
1. Select * from customer where name like 'a%'
2. Select * from customer where name like '%y'
3. Select * from customer where name like '%am%'
4. Select * from customer where name not like '%am%'
5. Select * from customer where name like '_arry'
6. Select * from customer where name like '_a__y'
7. Select * from customer where name like '[akm]%'
8. Select * from customer where name like '[a-c]%'
9. Select * from customer where name like '[^akm]%'
10. Select * from customer where name not like '[akm]%'
The IN operator allows you to specify multiple values in a WHERE clause.
Select * from customer where City in ('Bhopal', 'Indore')