SQL Queries: select top,Like,wildcard,Join,
The SQL SELECT TOP Clause
The SELECT TOP clause is used to specify the number of records to return.The SELECT TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.
Note: Not all database systems support the SELECT TOP clause.
SQL Server / MS Access Syntax
SELECT TOP number|percent column_name(s)
FROM table_name;
FROM table_name;
SQL SELECT TOP Equivalent in MySQL and Oracle
MySQL Syntax
SELECT column_name(s)
FROM table_name
LIMIT number;
FROM table_name
LIMIT number;
Example
SELECT *
FROM Persons
LIMIT 5;
FROM Persons
LIMIT 5;
Oracle Syntax
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
FROM table_name
WHERE ROWNUM <= number;
Example
SELECT *
FROM Persons
WHERE ROWNUM <=5;
FROM Persons
WHERE ROWNUM <=5;
Demo Database
In this tutorial we will use the well-known Northwind sample database.Below is a selection from the "Customers" table:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno TaquerÃa | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
SQL SELECT TOP Example
The following SQL statement selects the two first records from the "Customers" table:Example
SELECT TOP 2 * FROM Customers;
SQL SELECT TOP PERCENT Example
The following SQL statement selects the first 50% of the records from the "Customers" table:Example
SELECT TOP 50 PERCENT * FROM Customers;
The SQL LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.SQL LIKE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
FROM table_name
WHERE column_name LIKE pattern;
Demo Database
In this tutorial we will use the well-known Northwind sample database.Below is a selection from the "Customers" table:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno TaquerÃa | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
SQL LIKE Operator Examples
The following SQL statement selects all customers with a City starting with the letter "s":Example
SELECT * FROM Customers
WHERE City LIKE 's%';
WHERE City LIKE 's%';
The following SQL statement selects all customers with a City ending with the letter "s":
Example
SELECT * FROM Customers
WHERE City LIKE '%s';
WHERE City LIKE '%s';
Example
SELECT * FROM Customers
WHERE Country LIKE '%land%';
WHERE Country LIKE '%land%';
The following SQL statement selects all customers with a Country NOT containing the pattern "land":
Example
SELECT * FROM Customers
WHERE Country NOT LIKE '%land%';
WHERE Country NOT LIKE '%land%';
SQL Wildcard Characters
In SQL, wildcard characters are used with the SQL LIKE operator.SQL wildcards are used to search for data within a table.
With SQL, the wildcards are:
| Wildcard | Description |
|---|---|
| % | A substitute for zero or more characters |
| _ | A substitute for a single character |
| [charlist] | Sets and ranges of characters to match |
| [^charlist] or [!charlist] | Matches only a character NOT specified within the brackets |
Demo Database
In this tutorial we will use the well-known Northwind sample database.Below is a selection from the "Customers" table:
| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
|---|---|---|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
| 3 | Antonio Moreno TaquerÃa | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
| 4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
| 5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
Using the SQL % Wildcard
The following SQL statement selects all customers with a City starting with "ber":The following SQL statement selects all customers with a City containing the pattern "es":
Example
SELECT * FROM Customers
WHERE City LIKE '%es%';
WHERE City LIKE '%es%';
Using the SQL _ Wildcard
The following SQL statement selects all customers with a City starting with any character, followed by "erlin":Example
SELECT * FROM Customers
WHERE City LIKE '_erlin';
WHERE City LIKE '_erlin';
Example
SELECT * FROM Customers
WHERE City LIKE 'L_n_on';
WHERE City LIKE 'L_n_on';
Using the SQL [charlist] Wildcard
The following SQL statement selects all customers with a City starting with "b", "s", or "p":Example
SELECT * FROM Customers
WHERE City LIKE '[bsp]%';
WHERE City LIKE '[bsp]%';
Example
SELECT * FROM Customers
WHERE City LIKE '[a-c]%';
WHERE City LIKE '[a-c]%';
Example
SELECT * FROM Customers
WHERE City LIKE '[!bsp]%';
or
SELECT * FROM Customers
WHERE City NOT LIKE '[bsp]%';
WHERE City LIKE '[!bsp]%';
or
SELECT * FROM Customers
WHERE City NOT LIKE '[bsp]%';
SQL JOIN
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN return all rows from multiple tables where the join condition is met.
Let's look at a selection from the "Orders" table:
| OrderID | CustomerID | OrderDate |
|---|---|---|
| 10308 | 2 | 1996-09-18 |
| 10309 | 37 | 1996-09-19 |
| 10310 | 77 | 1996-09-20 |
| CustomerID | CustomerName | ContactName | Country |
|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Mexico |
| 3 | Antonio Moreno TaquerÃa | Antonio Moreno | Mexico |
Then, if we run the following SQL statement (that contains an INNER JOIN):
Example
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;
| OrderID | CustomerName | OrderDate |
|---|---|---|
| 10308 | Ana Trujillo Emparedados y helados | 9/18/1996 |
| 10365 | Antonio Moreno TaquerÃa | 11/27/1996 |
| 10383 | Around the Horn | 12/16/1996 |
| 10355 | Around the Horn | 11/15/1996 |
| 10278 | Berglunds snabbköp | 8/12/1996 |
Different SQL JOINs
Before we continue with examples, we will list the types the different SQL JOINs you can use:- INNER JOIN: Returns all rows when there is at least one match in BOTH tables
- LEFT JOIN: Return all rows from the left table, and the matched rows from the right table
- RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table
- FULL JOIN: Return all rows when there is a match in ONE of the tables
No comments:
Post a Comment