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

SQL Quries and Syntax

The document discusses various SQL SELECT statements for retrieving data from database tables. It covers selecting all columns or specific columns, using DISTINCT to remove duplicates, filtering with WHERE, sorting results with ORDER BY for one or multiple columns in ascending or descending order. It also notes that COUNT(DISTINCT) is not supported in Microsoft Access and provides a workaround.

Uploaded by

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

SQL Quries and Syntax

The document discusses various SQL SELECT statements for retrieving data from database tables. It covers selecting all columns or specific columns, using DISTINCT to remove duplicates, filtering with WHERE, sorting results with ORDER BY for one or multiple columns in ascending or descending order. It also notes that COUNT(DISTINCT) is not supported in Microsoft Access and provides a workaround.

Uploaded by

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

Return all the columns from the Customers table:

SELECT * FROM Customers;

Return data from the Customers table:

SELECT CustomerName, <column name> FROM Customers;

Select all the different countries from the "Customers" table:

SELECT DISTINCT Country FROM Customers;

If you omit the DISTINCT keyword, the SQL statement returns the "Country" value
from all the records of the "Customers" table:

SELECT Country FROM Customers;

By using the DISTINCT keyword in a function called COUNT, we can return the number
of different countries.

SELECT COUNT(DISTINCT Country) FROM Customers;

Note: The COUNT(DISTINCT column_name) is not supported in Microsoft Access


databases.

Here is a workaround for MS Access:

SELECT Count(*) AS DistinctCountries


FROM (SELECT DISTINCT Country FROM Customers);

Select all customers from Mexico:

SELECT * FROM Customers


WHERE Country='Mexico';

Note: The WHERE clause is not only used in SELECT statements, it is also used in
UPDATE, DELETE, etc.!

Sort the products by price:

SELECT * FROM Products


ORDER BY Price;

Sort the products from highest to lowest price:

SELECT * FROM Products


ORDER BY Price DESC;

Sort the products alphatbetically by ProductName:

SELECT * FROM Products


ORDER BY ProductName;

Sort the products by ProductName in reverse order:

SELECT * FROM Products


ORDER BY ProductName DESC;

ORDER BY Several Columns


SELECT * FROM Customers
ORDER BY Country, CustomerName;

Using Both ASC and DESC

SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC;

You might also like