You are on page 1of 5

SQL Lab

SELECT and FROM clauses

The SQL SELECT Statement


The SELECT statement is used to select data from a database.

The result is stored in a result table, called the result-set.

SQL SELECT Syntax


SELECT column_name, column_name //Select specified columns specified in FROM clause
FROM table_name;
SELECT * FROM table_name; // Selects all the columns from the table specified in FROM clause
FROM table_name;
Example:
SELECT * FROM Employee;
SELECT emp_id,emp_first_name,Emp_DOB FROM Employee;
SELECT emp_id,emp_first_name,Emp_DOB,salary,commission FROM Employee;

SQL SELECT DISTINCT Statement


The SELECT DISTINCT statement is used to return only distinct (different) values. In a
table, a column may contain many duplicate values; and sometimes you only want to list
the different (distinct) values.
The DISTINCT keyword can be used to return only distinct (different) values.

SQL SELECT DISTINCT Syntax


SELECT DISTINCT column_name,column_name
FROM table_name;

SELECT DISTINCT Emp_address FROM employee;

SQL WHERE Clause


The WHERE clause is used to extract only those records that fulfill a specified criterion. Select the subset of data.

SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;

Text Fields vs. Numeric Fields


SQL requires single quotes around text values (most database systems will also allow double quotes). However, numeric fields should not be
enclosed in quotes:
SELECT *
FROM Employee
WHERE emp_first_name=’aleem’;
SELECT emp_first_name,emp_last_name
FROM Employee
WHERE emp_id=1
The Operators
The AND operator displays a record if both the first condition AND the second condition are true.

The OR operator displays a record if either the first condition OR the second condition is true.

Logical Operator Condition1 Condition2 Final Result


AND True True Select Row
AND True False Reject Row
AND False True Reject Row
AND False False Reject Row
OR True True Select Row
OR True False Select Row
OR False True Select Row
OR False False Reject Row
NOT True Reject Row
NOT False Select Row
Comparison Operator
= Equal <> Not Equal
> Greater than < Less than
>= Greater than equal to <= Less than equal to
Arithmetic Operator Precedence
/,%,*,+,-

SELECT * FROM employee SELECT * FROM employee


WHERE emp_first_name='ali' WHERE emp_first_name='ali'
AND salary=50000; or salary=50000;
SELECT * SELECT *
FROM employee FROM employee
WHERE not emp_first_name='ali' WHERE emp_address='lahore'
AND(salary>=50000 OR commission is null);

SELECT emp_first_name,salary,Salary*2-50 SELECT emp_first_name,salary,Salary-50*2


FROM employee FROM employee

SELECT salary, Salary+commission SELECT salary, Salary+commission


FROM employee FROM employee
WHERE commission Is not Null;

The SQL ORDER BY Keyword


The ORDER BY keyword is used to sort the result-set by one or more columns. The ORDER BY keyword sorts the
records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword.

SQL ORDER BY Syntax

SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC;
Example:

SELECT emp_id,emp_first_name,emp_last_name SELECT


FROM employee emp_id,emp_first_name,emp_last_name
ORDER BY emp_first_name asc FROM employee
ORDER BY emp_first_name

SELECT emp_id,emp_first_name,emp_last_name SELECT


FROM employee emp_id,emp_first_name,emp_last_name
ORDER BY emp_first_name desc FROM employee
ORDER BY emp_id desc

SELECT
emp_id,emp_first_name,emp_last_name,emp_address
FROM employee
ORDER BY emp_first_name desc, emp_address asc

SQL Aliases
SQL aliases are used to give a database table or a column in a table, a temporary name. Basically aliases are created to make column names
more readable. We can create alias for both column and table level.

SQL Alias Syntax for Columns

SELECT column_name AS alias_name


FROM table_name;
Example

SELECT salary, Salary+commission as /// concatenation operator


TotalSalary SELECT emp_id,emp_first_name,emp_last_name
FROM employee , emp_first_name+" "+emp_last_name as
WHERE commission Is not Null; Full_Name
FROM employee

The IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.

SQL IN Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);

IN Operator Example
SELECT * SELECT *
FROM employee FROM employee
WHERE salary IN (50000, 100000); WHERE emp_first_name IN
('ali','imran','shahid');

SELECT emp_id, salary, commission SELECT emp_id, salary, commission,emp_address


FROM employee FROM employee
WHERE emp_id IN (5,7,9,8); WHERE emp_address IN ('lahore','multan');
The SQL BETWEEN Operator
The BETWEEN operator selects values within a range. The values can be numbers, text, or dates.

SQL BETWEEN Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

BETWEEN Operator Example

SELECT * FROM employee

WHERE salary BETWEEN 50000 AND 100000; ///inclusive range

BETWEEN Operator with Text Value Example

SELECT * FROM employee

WHERE emp_address BETWEEN 'a' AND 'M';

The SQL Like Operator


The LIKE operator is used to search for a specified pattern in a column.

SQL LIKE Syntax


SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
Example

//Starting with i followed by any character SELECT *

SELECT * FROM employee

FROM employee WHERE emp_first_name like"*i"

WHERE emp_first_name like"i*"

SELECT *

FROM employee

WHERE emp_first_name like"*i*"

Sample Table

Employee

Emp_I Emp_First Emp_Last_ Phone


Emp_Address Emp_DOB Salary Commission
d _Name Name _No
1 Imran saleem lahore 9/7/2015 123123 500000 100
2 ali zafar lahore 1/1/2000 125125 750000 250
3 irfan ali lahore 1/1/1990 125125 80000 300
4 asad shohail faisalabad 10/15/1995 126126 2000
5 Zeshan ahmed multan 10/10/1998 127127 45000
6 Ayesha ahmed Islamanabad 12/15/1999 128128 90000 10,000
7 aleem akhtar multan 12/15/2001 129129 50000
8 ali hussain islamabad 10/10/1998 130130 50000
9 shahid hussain rawalpindi 1/1/1999 131131 100000 200,00
10 anam ali Lahore 1/5/1997 132132 120000

You might also like