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

DBMS Select command usage

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

DBMS Select command usage

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

WEEK - 3

SELECT Command:
Examples on retrieving data from a single table using
i) SELECT statement
ii) SELECT statement with where clause (Comparison Operators, AND, OR,
NOT, IN, BETWEEN, LIKE)
iii) ORDER BY clause (sort by column name)
iv) LIMIT clause
Case 1:
i. SELECT:
The SELECT statement in MySQL is used to retrieve data from one or more
tables in a database. The basic syntax of a SELECT statement is as follows:
Syntax:
SELECT column1, column2, ...FROM table_name;
Case 2:
i. SELECT statement with where clause
SELECT statement with where clause syntax
SELECT column1, column2, ...FROM table_name WHERE condition;

ii. SELECT statement with WHERE clause using Comparison Operators.


In MySQL, and many other SQL databases, you can use various comparison
operators in the WHERE clause to filter rows based on specific conditions.
Here are some common comparison operators:

 =: Equal to
 != or <>: Not equal to
 <: Less than
 <=: Less than or equal to
 >: Greater than
 >=: Greater than or equal to
Equal to (=):
Used to check if two expressions are equal.
SELECT * FROM table_name WHERE column_name = value;
Not equal to (!= or <>):
Used to check if two expressions are not equal.
SELECT * FROM table_name WHERE column_name != value;
or
SELECT * FROM table_name WHERE column_name <> value;
Greater than (>):
Used to check if one expression is greater than another.
SELECT * FROM table_name WHERE column_name > value;
Less than (<):
Used to check if one expression is less than another.
SELECT * FROM table_name WHERE column_name < value;
Greater than or equal to (>=):
Used to check if one expression is greater than or equal to another.
SELECT * FROM table_name WHERE column_name >= value;
Less than or equal to (<=):
Used to check if one expression is less than or equal to another.
SELECT * FROM table_name WHERE column_name <= value;
IS NULL:
Used to check if a column contains a NULL value.
SELECT * FROM table_name WHERE column_name IS NULL;
IS NOT NULL:
Used to check if a column does not contain a NULL value.
SELECT * FROM table_name WHERE column_name IS NOT NULL;

iii. SELECT statement with WHERE clause using AND , OR ,NOT.


a. WHERE clause with AND.
SELECT column-names

FROM table-name

WHERE condition1 AND condition2

b. WHERE clause with OR

SELECT column1, column2, ...

FROM table_name

WHERE condition1 OR condition2;

c. WHERE clause with NOT

SELECT column1, column2, ...

FROM table_name

WHERE NOT condition;

d. SELECT statement with WHERE Clause Using IN operator

The IN operator in a WHERE clause is used to specify a range for multiple


values in a column. It allows you to match a value against a set of values. Here's
the syntax for a SELECT statement with the IN operator:

SELECT column1, column2, ...FROM table_name WHERE column_name IN


(value1, value2, ...);

Example:

SELECT employee_id, first_name, last_name, department

FROM employees

WHERE department IN ('Sales', 'Marketing');

e. SELECT statement with WHERE clause using BETWEEN Operator

The BETWEEN operator in a WHERE clause is used to filter rows based on a


range of values. It's commonly used with numeric, date, or timestamp columns.
Here's the syntax for a SELECT statement with the BETWEEN operator:
Syntax:

SELECT column1, column2, ...

FROM table_name

WHERE column_name BETWEEN value1 AND value2;

Example:

SELECT product_id, product_name, price

FROM products

WHERE price BETWEEN 50 AND 100;

f. SELECT statement with WHERE Clause using LIKE operator.

The LIKE operator in a WHERE clause is used to search for a specified


pattern in a column. It is often used with wildcard characters such as %
(percent) and _ (underscore). Here's the syntax for a SELECT statement with a
WHERE clause using the LIKE operator:

SELECT column1, column2, ...

FROM table_name

WHERE column_name LIKE pattern;

Example:

SELECT product_id, product_name

FROM products

WHERE product_name LIKE '_Laptop_';

Case 3:

iii) ORDER BY clause (sort by column name)

ORDER BY clause
The ORDER BY clause in a SELECT statement is used to sort the result set
based on one or more columns. Here's the basic syntax:

SELECT column1, column2, ...

FROM table_name

ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;

Example:

SELECT first_name, last_name, salary

FROM employees

ORDER BY salary DESC;

Case 4:

The LIMIT clause in a SELECT statement is used to restrict the number of


rows returned by a query. It is often used in combination with the ORDER BY
clause to get a specific subset of rows based on a certain order. Here's the basic
syntax:

SELECT column1, column2, ...

FROM table_name

ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...

LIMIT number_of_rows;

Example:

SELECT first_name, last_name, salary

FROM employees

ORDER BY salary DESC

LIMIT 5;

Viva Questions:

1. What is the purpose of the SELECT statement in SQL.


2. Can you retrieve all columns from a table using the SELECT statement?
How.
3. How do you use the DISTINCT keyword in a SELECT query.
4. What is the purpose of the WHERE clause in a SELECT statement.
5. What are comparison operators, and how are they used in the WHERE
clause.
6. What is the LIKE operator, and how is it used in SQL.
7. What is the purpose of the ORDER BY clause in SQL.
8. How do you sort query results in ascending or descending order.
9. What is the purpose of the LIMIT clause in SQL.
10. What is the syntax of a LIMIT clause.

You might also like