0% found this document useful (0 votes)
425 views3 pages

SQL Queries For Practice

This document provides an overview of key SQL syntax including: - SELECT statements to retrieve data from one or more columns or tables - DISTINCT clauses to retrieve only unique/distinct values - WHERE clauses with operators like =, <, > to filter records - AND, OR and NOT logical operators to combine conditions - ORDER BY to sort the returned records - INSERT statements to add new records to a table - NULL values and how to query for or exclude them

Uploaded by

Akshay Patil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
425 views3 pages

SQL Queries For Practice

This document provides an overview of key SQL syntax including: - SELECT statements to retrieve data from one or more columns or tables - DISTINCT clauses to retrieve only unique/distinct values - WHERE clauses with operators like =, <, > to filter records - AND, OR and NOT logical operators to combine conditions - ORDER BY to sort the returned records - INSERT statements to add new records to a table - NULL values and how to query for or exclude them

Uploaded by

Akshay Patil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 3

SELECT Syntax

1.SELECT column1, column2, ...FROM table_name;


2.SELECT * FROM table_name;
3.SELECT column FROM table_name;

SELECT DISTINCT Syntax---uniq values


1.SELECT DISTINCT column1, column2, ...FROM table_name;
2.SELECT COUNT(DISTINCT column) FROM table_name;
3.SELECT DISTINCT column FROM table_name;
4.SELECT COUNT(column) FROM table_name;
5.select count(column1),count(column2)from table_name;
6.select count(distinct CustomerName),count(PostalCode),count(distinct city)from
customers;

WHERE Syntax
1.SELECT column1, column2, ...FROM table_name WHERE condition;
2.SELECT column FROM table_name WHERE condition;

e.g.
1.SELECT * FROM Customers WHERE Country='Mexico';
2.SELECT * FROM Customers WHERE CustomerID=1;

Note:-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:

Operators in The WHERE Clause


Operator Description
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal. Note: In some versions of SQL this operator may be
written as !=
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column

e.g.
1.SELECT * FROM Products WHERE Price = 18;
2. SELECT * FROM Products WHERE Price > 30;
3.SELECT * FROM Products WHERE Price < 30;
4. SELECT * FROM Products WHERE Price >= 30;
5.SELECT * FROM Products WHERE Price <= 30;
6.SELECT * FROM Products WHERE Price <> 18;
7.SELECT * FROM Products WHERE Price BETWEEN 50 AND 60;
8.SELECT * FROM Customers WHERE City LIKE 's%';
9.SELECT * FROM Customers WHERE City IN ('Paris','London');

SQL AND, OR and NOT Operators


The AND and OR operators are used to filter records based on more than one
condition:
1.The AND operator displays a record if all the conditions separated by AND are
TRUE.
2.The OR operator displays a record if any of the conditions separated by OR is
TRUE.
3.The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax
SELECT column1, column2, ...FROM table_name WHERE condition1 AND condition2 AND
condition3 ...;
e.g -->SELECT * FROM Customers WHERE Country='Germany' AND City='Berlin';

OR Syntax
SELECT column1, column2, ...FROM table_name WHERE condition1 OR condition2 OR
condition3 ...;
e.g. SELECT * FROM Customers WHERE City='Berlin' OR City='München';
SELECT * FROM Customers WHERE Country='Germany' OR Country='Spain';

NOT Syntax
SELECT column1, column2, ...FROM table_name WHERE NOT condition;
e.g. SELECT * FROM Customers WHERE NOT Country='Germany';

Combining AND, OR and NOT e.g.


SELECT * FROM Customers WHERE Country='Germany' AND (City='Berlin' OR
City='München');
SELECT * FROM Customers WHERE NOT Country='Germany' AND NOT Country='USA';

The SQL ORDER BY Keyword


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

ORDER BY Syntax
SELECT column1, column2, ...FROM table_name ORDER BY column1, column2, ... ASC|
DESC;
e.g. SELECT * FROM Customers ORDER BY Country;
SELECT * FROM Customers ORDER BY Country DESC;
SELECT * FROM Customers ORDER BY Country, CustomerName;
SELECT * FROM Customers ORDER BY Country ASC, CustomerName DESC;

The SQL INSERT INTO Statement


The INSERT INTO statement is used to insert new records in a table.
It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:


INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2,
value3, ...);
e.g.1 INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,
Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
e.g.2 INSERT INTO Customers (CustomerName, City, Country) VALUES ('Cardinal',
'Stavanger', 'Norway');

SQL NULL Values


What is a NULL Value?
A field with a NULL value is a field with no value.
If a field in a table is optional, it is possible to insert a new record or update
a record without
adding a value to this field. Then, the field will be saved with a NULL value.

IS NULL Syntax
SELECT column_names FROM table_name WHERE column_name IS NULL;
e.g. SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS
NULL;

IS NOT NULL Syntax


SELECT column_names FROM table_name WHERE column_name IS NOT NULL;
SELECT CustomerName, ContactName, Address
FROM Customers WHERE Address IS NOT NULL;

You might also like