0% found this document useful (0 votes)
8 views

SQL material-2

The document provides an overview of SQL statements, including various operators such as arithmetic, comparison, and logical operators, along with examples of their usage. It also covers clauses like ORDER BY, GROUP BY, HAVING, DISTINCT, and LIMIT, explaining their syntax and purpose in SQL queries. Additionally, it highlights the use of aggregate functions and the differences between HAVING and WHERE clauses in SQL operations.

Uploaded by

Paramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL material-2

The document provides an overview of SQL statements, including various operators such as arithmetic, comparison, and logical operators, along with examples of their usage. It also covers clauses like ORDER BY, GROUP BY, HAVING, DISTINCT, and LIMIT, explaining their syntax and purpose in SQL queries. Additionally, it highlights the use of aggregate functions and the differences between HAVING and WHERE clauses in SQL operations.

Uploaded by

Paramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL statements

Operators:
Arithmetic operators:
+ - * / %
Examples:
create table employees(id int,name varchar(20),salary int,bonus int,tax int);
insert into employees values(124,'sunil',40000,10000,200);
select name,salary+bonus as totalsalary from employees;
select name,salary-tax as totalsalary from employees;
select name, salary*2 as totalsalary from employees;
select name, salary/2 as totalsalary from employees;
select name, salary%2 as totalsalary from employees;

Comparison Operators
= <> > < >= <=
select * from employees where salary=30000;
select * from employees where salary<>30000;

Logical Operators
1.AND
The AND operator allows for multiple conditions in an SQL statement's WHERE
clause.
Example:
select * from employees where salary>30000 and bonus=10000;
2.OR
The OR operator is used to combine multiple conditions in an SQL statement's
WHERE clause.
Example:
select * from employees where salary>30000 or bonus=10000;
3.BETWEEN
The BETWEEN operator is used to SQL queries for values that are within a set of
values, given the minimum value and the maximum value.
BETWEEN operator is used in the WHERE clause with select, update, delete, insert
SQL statements:

Example:
select * from employees where salary between 30000 and 40000;

4.IN
The IN operator is used to compare a value to a list of literal values that is specified.
It minimizes the use of SQL OR operator.
Example:
select * from employees where salary in(30000,40000);

5.LIKE
The LIKE is a logical operator in SQL.This SQL operator is used in the
WHERE clause with SELECT,UPDATE,DELETE Statements
It filters the records from the columns based on the pattern specified in the SQL
query
The wild cards(%) are used with like operator
Syntax:
SELECT column_Name1,column_Name2 FROM table_Name WHERE
column_name LIKE pattern;
Example:
select * from employees where name like 'a%' ;

6.NOT
The NOT operator reverses the logical operator with which it is used.
Example:
select * from employees where not name ='sunil' and bonus=10000;
7.IS NULL
The NULL operator is used to compare a value with a NULL value.
Example:
select * from employees where bonus is null;
select * from employees where bonus is not null;

ORDER BY clause:
The ORDER BY clause is used to sort the rows in ascending or descending order,
based on one or more columns
Syntax:
SELECT column_name FROM table_name [WHERE condition] [ORDER BY
column_name] [ASC | DESC];
Example:
select * from employees order by name asc;
select * from employees order by name desc;

functions:
count()
max()
min()
avg()
sum()
select count(salary) from employees;
select max(salary) from employees;
select min(salary) from employees;
select avg(salary) from employees;

Group by clause:
The GROUP BY clause is used along with the SELECT statement to arrange
identical data into groups.
The SELECT statement is used with the GROUP BY clause in the SQL query.
WHERE clause is placed before the GROUP BY clause in SQL.
ORDER BY clause is placed after the GROUP BY clause in SQL.
Group by is always used with functions

Syntax:
SELECT column_name FROM table_name WHERE [condition] GROUP BY
column_name ORDER BY column_name;
Example:
select count(name) from employees group by name;
Having clause:

The HAVING clause places the condition in the groups defined by the GROUP BY
clause in the SELECT statement.

This SQL clause is used after the 'GROUP BY' clause in the 'SELECT' statement.

This clause is used in SQL because we cannot use the WHERE clause with the SQL
aggregate functions. Both WHERE and HAVING clauses are used for filtering the
records in SQL queries.

HAVING WHERE

1. The HAVING clause is used in database systems 1. The WHERE clause is used in database systems
to fetch the data/values from the groups to fetch the data/values from the tables
according to the given condition. according to the given condition.

2. The HAVING clause is always executed with the 2. The WHERE clause can be executed without the
GROUP BY clause. GROUP BY clause.

3. The HAVING clause can include SQL aggregate 3. We cannot use the SQL aggregate function
functions in a query or statement. with WHERE clause in statements.

4. We can only use SELECT statement with 4. Whereas, we can easily use WHERE clause with
HAVING clause for filtering the records. UPDATE, DELETE, and SELECT statements.

5. The HAVING clause is used in SQL queries after 5. The WHERE clause is always used before the
the GROUP BY clause. GROUP BY clause in SQL queries.

6. It is used to filter groups. 8. It is used to filter the single record of the table.
Syntax:

SELECT column_Name1,column_Name2
aggregate_function_name(column_Name)FROM table_name GROUP BY
column_Name1 HAVING condition;
select count(*) from employees group by name having name='abhishek';

DISTINCT keyword :
DISTINCT keyword is used along with SELECT statement to eliminate the duplicate
records and fetching only unique records.
Syntax:
SELECT DISTINCT column_name FROM table_name WHERE [condition]
Example:
select distinct * from employees ;

LIMIT clause
Limit is used to specify the number of records to return.
Syntax:
SELECT column_name(s) FROM table_name WHERE condition LIMIT number;
Example:
select salary from myemployee limit 4;
syntax to know the nth hightest salary:
select salary from myemployee order by salary DESC limit 2,1;

You might also like