473,685 Members | 3,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the Query?

3 New Member
I have 3 tables student(s_id,s_ name),course(c_ id,c_name) and student_course( s_id(FK),c_id(F K)).
I would Like to select those students Name who have enrolled more then and equal to 3 courses.
What is the Query?
Dec 5 '09 #1
3 1615
debasisdas
8,127 Recognized Expert Expert
That seems to be a home work / assignment.

Kindly post the code that you have tried / working on.
Dec 5 '09 #2
nbiswas
149 New Member
Try this

First let's create some sample data

Expand|Select|Wrap|Line Numbers
  1. declare @student table(sid int, sname varchar(50))
  2. declare @course table(cid int, cname varchar(50))
  3. declare @student_course table(sid int, cid int)
  4.  
  5. insert into @student 
  6.     select 1,'student1' union all select 2,'student2' union all
  7.     select 3,'student3' union all select 4,'student4' union all
  8.     select 5,'student5' union all select 6,'student6'
  9. insert into @course 
  10.     select 101,'course1' union all select 202, 'course2' union all
  11.     select 303,'course3' union all select 404, 'course4' union all
  12.     select 505,'course5' union all select 606, 'course6' 
  13. insert into @student_course
  14.     select 1,101 union all select 1,202 union all select 1,303 union all
  15.     select 1,404 union all select 1,505 union all select 1,606 union all
  16.     select 2,101 union all select 2,505 union all select 2,606 union all
  17.     select 3,303 union all select 3,202 union all select 4,303 union all
  18.     select 4,404 union all select 4,202 union all select 5,101 union all 
  19.     select 6,101 union all select 6,202
Query 1:

Expand|Select|Wrap|Line Numbers
  1. select s.sid,s.sname from @student s
  2. inner join
  3. (    
  4. select sid,COUNT(cid) Courses from @student_course 
  5. group by sid
  6. having (COUNT(cid)>=3))X
  7. on X.sid = s.sid 
Query 2:
Expand|Select|Wrap|Line Numbers
  1. select sid,sname from @student where sid in(
  2. select sid from @student_course 
  3. group by sid
  4. having (COUNT(cid)>=3))
The output(in both the cases)

Expand|Select|Wrap|Line Numbers
  1. sid    sname
  2. 1    student1
  3. 2    student2
  4. 4    student4
Hope this helps
Dec 7 '09 #3
mzahid
3 New Member
Thank you very much sir.....
Dec 7 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
11437
by: J Sharman | last post by:
What is the easiest way to import an XML Datafeed from a URL into a MYSQL Database? Possibly using PHP Regards Joe PS Please answer to group and joe@joesharman.co.uk
4
2799
by: lawrence | last post by:
I'm very unhappy with the error message that I'm giving in this method: /** * 11-23-03 - getter * * We want to run the query against a MySql database and get back a
5
3048
by: lkrubner | last post by:
I have a webserver through Rackspace. I create a domain. I create an FTP user. I upload some files. I create a database called testOfSetupScript and then I create a database user named setup. I write some PHP code which should, I think, be able to to auto create the tables. The SQL looks like this:
0
1292
by: John Phelan | last post by:
I had a very large number of query statements that I had converted to SQL a long time ago aticipating that I would some day upsize my application. Every now and then when I do an import to a new application, some of the forms will import corrupted, and crashing the whole application, though it works fine in the old application. Today I did some experimenting on the two most recent corrupt form files. I discovered that even when I...
3
1414
by: White Bilky | last post by:
A97. Simplified description: I have a query which returns one field containing numbers. I have a table where one field contains the same numbers as the query (amongst others). The query and the table are not related in any way. I want to loop through each record in the query, find matching records in the table and delete them. The code counts the correct number of records
59
3171
by: Chris Dunaway | last post by:
The C# 3.0 spec (http://msdn.microsoft.com/vcsharp/future/) contains a feature called "Implicitly typed local variables". The type of the variable is determined at compile time based on the expression to the right of the = sign: var s = "Hello"; //s is strongly typed to be a string var i = 25; //i is strongly typed to be an integer But what I don't understand what advantage it is?
1
1718
by: Dot Net Daddy | last post by:
Hi, I am new to MS-SQL and I have a problem. Actually I have made similar queries on Oracle, but now I failed on MS Sql. First of all I am using SQL Server 2005, but I think it is not that much important because my question is related something fundamental. I want to compare the result (which should be random) in one table, with another one in another table and if they are same, I want to check the other information. In both tables...
2
2317
by: Aaron Reimann | last post by:
I have a lot of check boxes. This is an update of the check boxes, I want something was checked, then to do an insert (which is currently working), if something is no longer checked...delete the checkbox join/link that is in the database. So, the insert/checked is working, but the "unchecking" is not working. I don't know how to compare an array of what was not checked. Here is my code: if (is_array($_POST)) {
5
4137
by: Bob Bridges | last post by:
Start with two tables, parent records in one and child records in the other, a one-to-many relationship. Create a select statement joining the two. Display the query in datasheet mode. When I delete a row, only the child record is deleted from the source tables; the parent record is still there...which is what I wanted. Now display fields from that query in a continuous form. When I delete a record from that form, one of the child...
4
1856
bilibytes
by: bilibytes | last post by:
Hello i'm intrigued by what happens behind the scenes when I do a DB query to a database from php. I would like to know what is the exact life cycle of a mysql_query() I ask this question because i need to know what is the best solution to count the number of rows from a query. My query is performed to fetch records from a mysqlDb that will be displayed as a list of let's say 10 rows. If i hadn't put a sql LIMIT start, count the query...
0
8570
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8504
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9043
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5790
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4521
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.