WHERE clause can include additional logical operators and expressions that further limit the results returned : Limits « Select Clause « SQL / MySQL






WHERE clause can include additional logical operators and expressions that further limit the results returned

      
by the SELECT statement.
mysql>
mysql> CREATE TABLE Books
    -> (
    ->     BookID SMALLINT NOT NULL PRIMARY KEY,
    ->     BookTitle VARCHAR(60) NOT NULL,
    ->     Copyright YEAR NOT NULL
    -> )
    -> ENGINE=INNODB;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO Books VALUES
    -> (12786, 'Notebook', 1934),
    -> (13331, 'C++', 1919),
    -> (14356, 'Opera', 1966),
    -> (15729, 'Sql Server', 1932),
    -> (16284, 'C', 1996),
    -> (17695, 'Pascal', 1980),
    -> (19264, 'Postcards', 1992),
    -> (19354, 'Oracle', 1993);
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> CREATE TABLE Authors
    -> (
    ->     AuthID SMALLINT NOT NULL PRIMARY KEY,
    ->     AuthFN VARCHAR(20),
    ->     AuthMN VARCHAR(20),
    ->     AuthLN VARCHAR(20)
    -> )
    -> ENGINE=INNODB;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO Authors VALUES
    -> (1006, 'Hunter', 'S.', 'Thompson'),
    -> (1007, 'Joyce', 'Carol', 'Oates'),
    -> (1008, 'Black', NULL, 'Elk'),
    -> (1009, 'Rainer', 'Maria', 'Rilke'),
    -> (1010, 'John', 'Kennedy', 'Toole'),
    -> (1011, 'John', 'G.', 'Neihardt'),
    -> (1012, 'Annie', NULL, 'Proulx'),
    -> (1013, 'Alan', NULL, 'Watts'),
    -> (1014, 'Nelson', NULL, 'Algren');
Query OK, 9 rows affected (0.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

mysql>
mysql>
mysql> CREATE TABLE AuthorBook
    -> (
    ->     BookID SMALLINT NOT NULL,
    ->     AuthID SMALLINT NOT NULL,
    ->     PRIMARY KEY (AuthID, BookID)
    -> )
    -> ENGINE=INNODB;
mysql>
mysql> INSERT INTO AuthorBook VALUES
    -> (1006, 14356),
    -> (1008, 15729),
    -> (1009, 12786),
    -> (1010, 17695),
    -> (1011, 15729),
    -> (1012, 19264),
    -> (1012, 19354),
    -> (1014, 16284);
mysql>
mysql>
mysql> SELECT BookTitle, Copyright, ab.AuthID
    -> FROM Books AS b, AuthorBook AS ab
    -> WHERE b.BookID=ab.BookID AND Copyright<1980
    -> ORDER BY BookTitle;
Empty set (0.00 sec)

mysql>
mysql> drop table AuthorBook;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table Authors;
Query OK, 0 rows affected (0.00 sec)

mysql> drop table Books;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
    
  








Related examples in the same category

1.Limit the query results: Get 3 Most Expensive Products
2.Use LIMIT in SELECT
3.Use LIMIT
4.Another ORDER BY and limit
5.Simple LIMIT
6.Retrieving the Top Five Students
7.ORDER and limit
8.Limits and order
9.Scrolling through the entire product table four records at a time
10.Determining the Number of Records Suppressed by LIMIT (SQL_CALC_FOUND_ROWS, FOUND_ROWS)
11.Limiting a Selection Using LIMIT
12.Use ORDER BY to sort the result set. Then you can use LIMIT to find smallest and largest values.
13.Order the rows with the largest SUM( ) values first and use LIMIT to select the first record
14.A DELETE statement can be qualified by using an ORDER BY and a LIMIT clause
15.The LIMIT clause takes two arguments, as the following syntax shows: LIMIT [,]
16.SELECT statement includes a LIMIT clause that specifies an offset value of 3 and a row count value of 4
17.Limit to first three columns
18.LIMIT 5 OFFSET 3
19.Delete with order by and limit clause
20.Pulling a Section from the Middle of a Result Set
21.Tell MySQL what offset to use, from which result to start limiting.
22.The default offset is 0, so by specifying an offset of 1, you're taking the second record.
23.Selecting Records from the Beginning or End of a Result Set
24.Processing the First or Last n Records
25.Limiting the Number of Results
26.But LIMIT allows you to return only that record