Subqueries in the WHERE Clause 2 : Sub query « Select Clause « SQL / MySQL






Subqueries in the WHERE Clause 2

   
/*
mysql> select * from Course;
+----------+----------------------+---------+
| CourseID | Name                 | Credits |
+----------+----------------------+---------+
|        1 | Mediaeval Romanian   |       5 |
|        2 | Philosophy           |       5 |
|        3 | History of Computing |       5 |
+----------+----------------------+---------+
3 rows in set (0.01 sec)

mysql> SELECT Name FROM Course
    -> WHERE CourseID =
    -> (
    -> SELECT CourseID from EXAM
    -> WHERE SustainedOn='10-MAR-03'
    -> );
Empty set, 1 warning (0.00 sec)


*/

Drop TABLE Course;
Drop TABLE Exam;       
       
CREATE TABLE Course (
   CourseID INT NOT NULL PRIMARY KEY,
   Name     VARCHAR(50),
   Credits  INT)
TYPE = InnoDB;

CREATE TABLE Exam (
   ExamID      INT NOT NULL PRIMARY KEY,
   CourseID    INT NOT NULL,
   SustainedOn DATE,
   Comments    VARCHAR(255),

   INDEX       examcourse_index(CourseID)
   
 
)TYPE = InnoDB;


INSERT INTO Course (CourseID,Name,Credits) VALUES (1,'Mediaeval Romanian',5);
INSERT INTO Course (CourseID,Name,Credits) VALUES (2,'Philosophy',5);
INSERT INTO Course (CourseID,Name,Credits) VALUES (3,'History of Computing',5);

INSERT INTO Exam (ExamID,CourseID,SustainedOn,Comments) VALUES 
      (1,1,'2003-03-12','JavaScript');
      
INSERT INTO Exam (ExamID,CourseID,SustainedOn,Comments) VALUES 
      (2,1,'2003-03-13','Java');
      
INSERT INTO Exam (ExamID,CourseID,SustainedOn,Comments) VALUES 
      (3,2,'2003-03-11','Python');
      
INSERT INTO Exam (ExamID,CourseID,SustainedOn) VALUES 
      (4,3,'2003-03-18','Swing');

select * from Course;

SELECT Name FROM Course
WHERE CourseID =
(
SELECT CourseID from EXAM
WHERE SustainedOn='10-MAR-03'
);

           
         
    
    
  








Related examples in the same category

1.Subqueries As Calculated Columns: Simple Subqueries
2.Subqueries That Return Multiple Results
3.Sub queries
4.Sub query with string concatenate
5.Sub query with not equal and order
6.Sub query with calculation
7.Update command with sub query
8.Delete command with sub query
9.SubSELECTs Syntax Variants
10.Uses a subquery to return an AuthID value
11.Use a not equal (<>) comparison operator in the WHERE clause to introduce the subquery
12.Subquery uses an aggregate function to arrive at a value that the outer statement can use
13.Nested subquery
14.Four-level nested subquery with alias
15.Nested subquery and where clause
16.Minus value from subquery
17.Subquery and equals operator(ERROR 1242 (21000): Subquery returns more than 1 row)
18.Row values and subquery
19.Nested three level subquery
20.Greater than the result of subquery
21.Equals to the result of subquery
22.Row values comparison for subquery
23.Select from the result of subquery
24.Value calculation of subquery
25.Subquery as a table
26.Alias subquery
27.Calculation in subquery
28.Using function with result from subquery
29.Create constant value in subquery
30.Calculation in subquery and use the result from outside
31.Equals operator with subquery
32.Less than and subquery(ERROR 1242 (21000): Subquery returns more than 1 row)
33.Less than data type value from subquery
34.<=> and subquery
35.Less than two values in subquery
36.Greater than two values in subquery
37.Get substring for a value from subquery
38.Mix constant value and subquery
39.Using the value from subquery with in operator
40.Pair value within in operator and subquery
41.In a list of value from subquery
42.In operator and subquery
43.Nested subquery and in operator
44.Not in and subquery
45.Between...and opertor and subquery
46.Count value in subquery
47.Add up count value from subquery
48.From a subquery
49.Aggregate function in subquery
50.Having clause with subquery
51.Count and subquery in Having clause
52.Subquery with having clause
53.Order by value from subquery
54.Subquery with limit clause
55.Insert statement with subquery
56.Use a derived table.
57.Searching for Titles Without Authors
58.Compare to aggregate function
59.Data calculation inside in operator
60.COMPARISON OPERATORS WITH SUBQUERIES
61.Select from three subqueries
62.Select from two subqueries
63.Row value comparison with select statement
64.Compare two data type value together
65.Nested subqueries
66.Two USING clauses, rather than ON clauses
67.Finding people who didn't send mail to themselves