Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

SQL Commands

Uploaded by

Sudha G
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

SQL Commands

Uploaded by

Sudha G
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

SQL COMMANDS

List of SQL Commands


SELECT
. It allows you to define what data you want your query to return.
It is a part of a query determines which columns of the data to show in the results. There are also options you can
apply to show data that is not a table column.

SELECT name FROM customers;

SELECT *
SELECT used with an asterisk (*) will return all of the columns in the table we're
querying.

SELECT * FROM customers;


SELECT DISTINCT
SELECT DISTINCT only returns data that is distinct — in other words, if there are duplicate
records, it will return only one copy of each.

SELECT distinct name FROM customers;

SELECT INTO
SELECT INTO copies the specified data from one table into another.

SELECT * INTO customers FROM


customers_backup;
SELECT TOP
SELECT TOP only returns the top x number or percent from a table.
SELECT TOP 50 * FROM customers;

AS
AS renames a column or table with an alias that we can choose.

SELECT name AS first_name FROM customers;


FROM
FROM specifies the table we're pulling our data from:

SELECT name FROM customers;

WHERE
WHERE filters your query to only return results that match a set condition.
We can use this together with conditional operators like =, >, <, >=, <=, etc.

SELECT name FROM customers where name=‘banu’;


AND
AND combines two or more conditions in a single query. All of the conditions must be
met for the result to be returned.

SELECT name FROM customers where name=‘banu’ AND age=56;

OR
OR combines two or more conditions in a single query. Only one of the conditions
must be met for a result to be returned.

SELECT name FROM customers where name=‘banu’ OR age=56;


BETWEEN
BETWEEN filters your query to return only results that fit a specified range

SELECT name FROM customers WHERE age BETWEEN 45 AND 55;

LIKE
LIKE searches for a specified pattern in a column. In the example code below, any row
with a name that included the characters Bob would be returned.

SELECT name FROM customers WHERE name LIKE ‘%Banu%’;


Other operators for LIKE:
•%x — will select all values that begin with x
•%x% — will select all values that include x
•x% — will select all values that end with x
•x%y — will select all values that begin with x and end with y
•_x% — will select all values have x as the second character
•x_% — will select all values that begin with x and are at least two characters
long. You can add additional _ characters to extend the length requirement,
i.e. x___%
1)Display the details of all employees

2)Display the depart informaction from department table

3)Display the name and job for all the employees

4)Display the name and salary for all the employees

5)Display the employee no and totalsalary for all the employees

6)Display the employee name and annual salary for all employees.

7)Display the names of all the employees who are working in depart number 10.

8)Display the names of all the employees who are working as clerks and drawing a salary more than
3000.

9)Display the employee number and name who are earning comm.

10)Display the employee number and name who do not earn any comm.
IN
IN allows us to specify multiple values we want to select for when using the
WHERE command.

SELECT name FROM customers WHERE name IN (‘Banu’, ‘Farida’, ‘Hari’);

IS NULL
IS NULL will return only rows with a NULL value.

SELECT name FROM customers WHERE name IS NULL;


IS NOT NULL
IS NOT NULL does the opposite — it will return only rows without a NULL
value.
SELECT name FROM customers WHERE name IS NOT NULL;

UPDATE
The UPDATE statement is used to update data in a table.

UPDATE customers SET age = 56 WHERE name = ‘Banu’;


DELETE
DELETE can remove all rows from a table (using ), or can be used as part of
a WHERE clause to delete rows that meet a specific condition.

DELETE FROM customers WHERE name = ‘Bob’;

GROUP BY
The GROUP BY statement groups rows with the same values into summary
rows. The statement is often used with aggregate functions. For example,
the code below will display the average age for each name that appears in
our customers table.

SELECT name, AVG(age) FROM customers GROUP BY name;


HAVING
HAVING performs the same action as the WHERE clause. The difference is
that HAVING is used for aggregate functions, whereas WHERE doesn’t work
with them.
To return the number of rows for each name, but only for names with more
than 2 records.

SELECT COUNT(customer_id), name FROM customers GROUP BY name


HAVING COUNT(customer_id) > 2;

ORDER BY
ORDER BY sets the order of the returned results. The order will be
ascending by default.
SELECT name FROM customers ORDER BY age;

DESC
DESC will return the results in descending order.

SELECT name FROM customers ORDER BY age DESC;


JOINS (INNER, LEFT, RIGHT, FULL)
A JOIN clause is used to combine rows from two or more tables. The four
types of JOIN are INNER, LEFT, RIGHT and FULL.

INNER JOIN
INNER JOIN selects records that have matching values in both tables.

SELECT name FROM customers INNER JOIN orders ON


customers.customer_id = orders.customer_id;
LEFT JOIN
LEFT JOIN selects records from the left table that match records in the right
table

SELECT name FROM customers LEFT JOIN orders ON


customers.customer_id = orders.customer_id;

RIGHT JOIN
RIGHT JOIN selects records from the right table that match records in the
left table.

ELECT name FROM customers RIGHT JOIN orders ON


customers.customer_id = orders.customer_id;
FULL JOIN
FULL JOIN selects records that have a match in the left or right table. Think
of it as the “OR” JOIN compared with the “AND” JOIN (INNER JOIN).

SELECT name FROM customers FULL OUTER JOIN orders ON


customers.customer_id = orders.customer_id;

EXISTS
EXISTS is used to test for the existence of any record in a subquery.

SELECT name FROM customers WHERE EXISTS (SELECT order FROM


ORDERS WHERE customer_id = 1);
TRUNCATE
TRUNCATE TABLE removes all data entries from a table in a database, but
keeps the table and structure in place. Similar to DELETE.

TRUNCATE TABLE customers;

UNION
UNION combines multiple result-sets using two or more SELECT
statements and eliminates duplicate rows.

SELECT name FROM customers UNION


SELECT name FROM orders;
32)Display the maximum salary being paid to depart number 20.

33)Display the minimum salary being paid to any SALESMAN.

34)Display the average salary drawn by MANAGERS.

35)Display the total salary drawn by ANALYST working in depart number 40.

36)Display the names of the employee in order of salary i.e the name of the employee earning lowest
salary should salary appear first.

37)Display the names of the employee in descending order of salary.

38)Display the names of the employee in order of employee name.

39)Display empno,ename,deptno,sal sort the output first base on name and within name by deptno and
with in deptno by sal.

40).The name of the employee earning highest annual salary should apper first.
11)display the names of employees who are working as clerks,salesman or analyst and drawing a
salary more than 3000.

13)Display the list of employcees who have joined the company before
30-JUN-90 or after 31-DEC-90.

14)Display current Date.

15)Display the list of all users in your database(use catalog table).

18)Display the names of employees working in depart number 10 or 20 or 40 or mployees working as


CLERKS, SALESMAN or ANALYST.
a) Select ename from emp where deptno in(10,20,40) or job in('CLERKS','SALESMAN','ANALYST');

19) Display the names of employees whose name starts with alphabet S.

20) Display the Employee names for employees whose name ends with Alphabet S.

31)Display the maximum salary being paid to CLERK.


Title Lorem Ipsum

LOREM IPSUM DOLOR SIT AMET, NUNC VIVERRA IMPERDIET PELLENTESQUE HABITANT
CONSECTETUER ADIPISCING ENIM. FUSCE EST. VIVAMUS A MORBI TRISTIQUE SENECTUS ET
ELIT. TELLUS. NETUS.

You might also like