Sunday, October 18, 2015

SELECT TOP Clause,

SQL Server / MS Access Syntax


SELECT TOP number|percent column_name(s)
FROM table_name;



SQL SELECT TOP Equivalent in MySQL and Oracle


MySQL Syntax


SELECT column_name(s)
FROM table_name
LIMIT number;

Example


SELECT *
FROM Persons
LIMIT 5;
Using the SQL % Wildcard The following SQL statement selects all customers with a City starting with "ber": Example SELECT * FROM Customers WHERE City LIKE 'ber%';

Oracle Syntax


SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;

Example


SELECT *
FROM Persons
WHERE ROWNUM <=5;

Example


SELECT TOP 2 * FROM Customers; 

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;

Example


SELECT * FROM Customers
WHERE City LIKE 's%'; 
CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
Blondel père et fils  Frédérique Citeaux  24, place Kléber  Strasbourg  67000  France 
15  Comércio Mineiro  Pedro Afonso  Av. dos Lusíadas, 23  São Paulo  05432-043  Brazil 
21  Familia Arquibaldo  Aria Cruz  Rua Orós, 92  São Paulo  05442-030  Brazil 
30  Godos Cocina Típica  José Pedro Freyre  C/ Romero, 33  Sevilla  41101  Spain 
35  HILARIÓN-Abastos  Carlos Hernández  Carrera 22 con Ave. Carlos Soublette #8-35  San Cristóbal  5022  Venezuela 

Example


SELECT * FROM Customers
WHERE City LIKE '%s'; 
ustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
12  Cactus Comidas para llevar  Patricio Simpson  Cerrito 333  Buenos Aires  1010  Argentina 
18  Du monde entier  Janine Labrune  67, rue des Cinquante Otages  Nantes  44000  France 
26  France restauration  Carine Schmitt  54, rue Royale  Nantes  44000  France 
31  Gourmet Lanchonetes  André Fonseca  Av. Brasil, 442  Campinas  04876-786  Brazil 
33  GROSELLA-Restaurante  Manuel Pereira  5ª Ave. Los Palos Grandes  Caracas  1081  Venezuela 
38  Island Trading  Helen Bennett  Garden House Crowther Way  Cowes  PO31 7PJ  UK 
40  La corne d'abondance  Daniel Tonini  67, avenue de l'Europe  Versailles  78000  France 
50  Maison Dewey  Catherine Dewey  Rue Joseph-Bens 532  Bruxelles  B-1180  Belgium 
54  Océano Atlántico Ltda.  Yvonne Moncada  Ing. Gustavo Moncada 8585 Piso 20-A  Buenos Aires  1010  Argentina 
57  Paris spécialités  Marie Bertrand  265, boulevard Charonne  Paris  75012  France 
64  Rancho grande  Sergio Gutiérrez  Av. del Libertador 900  Buenos Aires  1010  Argentina 
74  Spécialités du monde  Dominique Perrier  25, rue Lauriston  Paris  75016  France 
83  Vaffeljernet  Palle Ibsen  Smagsløget 45  Århus  8200  Denmark 

Example


SELECT * FROM Customers
WHERE Country LIKE '%land%'; 
CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
14  Chop-suey Chinese  Yang Wang  Hauptstr. 29  Bern  3012  Switzerland 
37  Hungry Owl All-Night Grocers  Patricia McKenna  8 Johnstown Road  Cork  Ireland 
68  Richter Supermarkt  Michael Holz  Grenzacherweg 237  Genève  1203  Switzerland 
87  Wartian Herkku  Pirkko Koskitalo  Torikatu 38  Oulu  90110  Finland 
90  Wilman Kala  Matti Karttunen  Keskuskatu 45  Helsinki  21240  Finland 
91  Wolski  Zbyszek  ul. Filtrowa 68  Walla  01-012  Poland 
he following SQL statement selects all customers with Country NOT containing the pattern "land":

Example


SELECT * FROM Customers
WHERE Country NOT LIKE '%land%'; 

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
Alfreds Futterkiste  Maria Anders  Obere Str. 57  Berlin  12209  Germany 
Ana Trujillo Emparedados y helados  Ana Trujillo  Avda. de la Constitución 2222  México D.F.  05021  Mexico 
Antonio Moreno Taquería  Antonio Moreno  Mataderos 2312  México D.F.  05023  Mexico 
Around the Horn  Thomas Hardy  120 Hanover Sq.  London  WA1 1DP  UK 
Berglunds snabbköp  Christina Berglund  Berguvsvägen 8  Luleå  S-958 22  Sweden 
Blauer See Delikatessen  Hanna Moos  Forsterstr. 57  Mannheim  68306  Germany 
Blondel père et fils  Frédérique Citeaux  24, place Kléber  Strasbourg  67000  France 
Bólido Comidas preparadas  Martín Sommer  C/ Araquil, 67  Madrid  28023  Spain 
Bon app'  Laurence Lebihans  12, rue des Bouchers  Marseille  13008  France