473,386 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Basic SQL Question

I have a question on a practice assignment that I can't solve. Can someone
help me out?

Question:

The table Arc(x,y) currently has the following tuples (note there are
duplicates): (1,2), (1,2), (2,3), (3,4), (3,4), (4,1), (4,1), (4,1), (4,2).
Compute the result of the query:

SELECT a1.x, a2.y, COUNT(*)
FROM Arc a1, Arc a2
WHERE a1.y = a2.x
GROUP BY a1.x, a2.y;

Which of the following tuples is in the result?
a) (2,3,2)
b) (2,4,6)
c) (4,2,6)
d) (3,2,6)

Jul 23 '05 #1
5 3300
KGuy wrote:
I have a question on a practice assignment that I can't solve. Can someone
help me out?


Yes, create the table in question to your database, insert the given
values to there and then execute the given query and check which of
given results matches to the actual result.

This task is so simple that you don't even need brains to solve it,
since all you have to do is follow the instructions, compare few rows
and tell what you see.

If you don't have database, you can get one for free, for example mysql:
http://www.mysql.com/
Jul 23 '05 #2
> Yes, create the table in question to your database, insert the given
values to there and then execute the given query and check which of given
results matches to the actual result.


Of course, I could do that, but I would like to understand why the output is
what it is. Sorry if I was unclear. Thanks for the reply.

-Imran
Jul 23 '05 #3
KGuy wrote:
Of course, I could do that


Don't say you could do that, just do it. When you tell the corrent
answer, someone might be able explain it. And if want to make a guess,
be sure not choose wrong one.

Please understand that if we just give the correct answers it would be
the same as just shooting you in the head. It would do you more harm
than good. Point of practise assignments is that you learn by doing them.
Jul 23 '05 #4
>I have a question on a practice assignment that I can't solve. Can someone
help me out?


Thanks everybody. I managed to solve it by hand using tips from someone
(Andy Hassall). It takes a while, but at least it's doable and I understand
it. If the answer is important to you, reply to this post.
Jul 23 '05 #5
On Sun, 23 Jan 2005 13:00:20 -0500, KGuy wrote:

(crossposting removed)
I have a question on a practice assignment that I can't solve. Can someone
help me out?
Hi KGuy,

In a few weeks time, you'll have a test. If you don't learn to work out
your assignments now, you'll certainly fluke the test.

And if you're lucky and pass the test, you'll be in even more trouble when
you're hired and you have to debug some real SQL.

Question:

The table Arc(x,y) currently has the following tuples (note there are
duplicates) (snip)

If there are duplicates, you don't have a table at all. A collection of
data that may hold duplicates is a heap. I'm truly amazed that there are
still schools where SQL is taught with text books that don't include a
primary key on every table in every example or every assignment.

(1,2), (1,2), (2,3), (3,4), (3,4), (4,1), (4,1), (4,1), (4,2).
Compute the result of the query:

SELECT a1.x, a2.y, COUNT(*)
FROM Arc a1, Arc a2
WHERE a1.y = a2.x
GROUP BY a1.x, a2.y;
Almost all professionals prefer the (more verbose, but better documenting)
infixed join notation. For outer join, the infixed notation is the only
way to avoid ambiguity. For inner joins, beth versions are allowed, but
the infixed notation is more popular. Also, avoiding the optional AS
between table name and table alias is not recommended either!

SELECT a1.x, a2.y, COUNT(*)
FROM Arc AS a1
INNER JOIN Arc AS a2
ON a1.y = a2.x
GROUP BY a1.x, a2.y;

This is how the query should (IMO) appear in a decent studybook.

Which of the following tuples is in the result?
a) (2,3,2)
b) (2,4,6)
c) (4,2,6)
d) (3,2,6)


Easy to work out, actually. As an example, I'll show you why the answer
isn't a. You can then work out the three remaining options.

Each row in the output that shows 2 as the first value has a1.x=2. This
must stem from the row (2, 3), as that is the only row with an x value of
2. The join condition (a1.y=a2.x) means that the a2 row must have an x
value of 3 (as the y value in the a1 row is 3). Two rows qualify: (3, 4)
and (3, 4). Both have an y value of 4, so before grouping, there are 2
rows with a1.x=2 and a2.y=4. After grouping, this is 1 group with a row
count of 2. The result set should contain (2, 4, 2) as the only row
starting with 2.

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: pauldepstein | last post by:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They construct a vector class which contains the variable vec, a float* variable where the length of the array (number of...
6
by: DH | last post by:
I have a VERY basic question about figuring database size. I've inherited a database which is generally similar to this basic one: Item, Red, Blue, Green, Yellow (text), (int),(int),(int),(int)...
9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
1
by: frankhanretty | last post by:
Do I have to install Visual basic on the remote terminals as I did on the server? I have an visual basic 5 application running fine on my client's server and he is now networked. He wants to run the...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.