Open Side Menu Go to the Top
Register
Help with SQL (and/or Excel) Help with SQL (and/or Excel)

11-04-2014 , 08:45 AM
Hi,

I have an assignment due soon, which I've been having large problems with.

I'm at a complete loss as of what to do. I'm supposed to write some SQL-queries to get some information from a database (that I have).

But I just don't understand how to write these queries.

If anyone would be able to help it would be greatly appreciated.

Please send a PM.

Thanks
Help with SQL (and/or Excel) Quote
11-04-2014 , 09:48 AM
You'll probably get more/better help if you actually elaborate here in this thread. Maybe start with what you have in your database and what you need to get out of it (simplest question first).
Help with SQL (and/or Excel) Quote
11-04-2014 , 10:32 AM
we have a database for a company, Company. However this database isn't like others we've used before.

Previously we had databases with simple stuff like students, courses etc.

From those I could see stuff using SELECT * FROM students however I don't know how to find anything from this new database. I think it has something to do with meta data, but honestly I don't understand the lectures etc.

One of the questions I have is "who (if any I guess) of the employees has been sick".

However there's endlessly more tables in this database, and I'm at a loss.

If you need more specifics let me know.

EDIT: Might be a RDBMS database
Help with SQL (and/or Excel) Quote
11-04-2014 , 12:43 PM
at minimum, you need to post the schema and the questions you're supposed to answer via queries.
Help with SQL (and/or Excel) Quote
11-04-2014 , 07:33 PM
Quote:
Originally Posted by nejo
One of the questions I have is "who (if any I guess) of the employees has been sick".
I assume there is an employees table. And I assume there is another table with data on when people call in sick. So it would be something like

SELECT EmployeeID FROM EmployeeTable e
INNER JOIN SickPeopleTable s ON e.EmployeeID = s.EmployeeID
Help with SQL (and/or Excel) Quote
11-04-2014 , 08:07 PM
Quote:
Originally Posted by splashpot
I assume there is an employees table. And I assume there is another table with data on when people call in sick. So it would be something like

SELECT EmployeeID FROM EmployeeTable e
INNER JOIN SickPeopleTable s ON e.EmployeeID = s.EmployeeID
I wrote it like this:

SELECT DISTINCT No_ as EmployeeID, e.[First Name] as "First Name", e.[Last Name] as "Last Name",
eab."Employee No_"
FROM Employee e ,
[Employee Absence] eab
WHERE e.No_= eab.[Employee No_]
AND eab.[Cause of Absence Code]='SICK'


I believe mine worked, on upon a brief inspection was OK:ed by a tutor(the one who helps the professor out at universities).

What's the difference between how you wrote it and how I wrote it?

I didn't join tables, but instead checked two tables for matching EmpID.

Any thoughts and/or input is appreciated, trying to figure out how all of this works.

//nejo
Help with SQL (and/or Excel) Quote
11-04-2014 , 08:11 PM
Not much difference. Inner joins are more common and more readable I think.

http://stackoverflow.com/questions/1...-join-vs-where
Help with SQL (and/or Excel) Quote
11-07-2014 , 11:39 AM
I have written all of my queries, and now I'm trying to reformat them to fit in Excel.

My original query looks like this;

SELECT DISTINCT No_ as EmployeeID, e.[First Name] as "Förnamn", e.[Last Name] as Efternamn,
eab."Employee No_"
FROM "dbEmployee" e ,
"dbEmployee Absence" eab
WHERE e.No_= eab.[Employee No_]
AND eab.[Cause of Absence Code]='SJUK'

For Excel to understand I have to write, for instance, [First Name] as "MyDatabase.dbEmployee"."First Name" or something like that. And when I do that, I cannot use e.[First Name] on that code. This wouldn't be runnabel: e."MyDatabase.dbEmployee"."First Name"

But for queries where I compare stuff to eachother I have to have the e. possibility.

Any ideas?
Help with SQL (and/or Excel) Quote
11-07-2014 , 01:07 PM
Quote:
Originally Posted by nejo
I have written all of my queries, and now I'm trying to reformat them to fit in Excel.

My original query looks like this;

SELECT DISTINCT No_ as EmployeeID, e.[First Name] as "Förnamn", e.[Last Name] as Efternamn,
eab."Employee No_"
FROM "dbEmployee" e ,
"dbEmployee Absence" eab
WHERE e.No_= eab.[Employee No_]
AND eab.[Cause of Absence Code]='SJUK'

For Excel to understand I have to write, for instance, [First Name] as "MyDatabase.dbEmployee"."First Name" or something like that. And when I do that, I cannot use e.[First Name] on that code. This wouldn't be runnabel: e."MyDatabase.dbEmployee"."First Name"

But for queries where I compare stuff to eachother I have to have the e. possibility.

Any ideas?
First of all, why are you mixing "" with []? It makes it super hard to read.

Second, I have no idea what you're trying to do in Excel, but
Quote:
I have to write, for instance, [First Name] as "MyDatabase.dbEmployee"."First Name" or something like that. And when I do that, I cannot use e.[First Name] on that code.
SELECT e.[First Name] FROM MyDatabase.dbEmployee e

should work.
Help with SQL (and/or Excel) Quote
11-07-2014 , 11:54 PM
Quote:
Originally Posted by nejo
I wrote it like this:

SELECT DISTINCT No_ as EmployeeID, e.[First Name] as "First Name", e.[Last Name] as "Last Name",
eab."Employee No_"
FROM Employee e ,
[Employee Absence] eab
WHERE e.No_= eab.[Employee No_]
AND eab.[Cause of Absence Code]='SICK'

I believe mine worked, on upon a brief inspection was OK:ed by a tutor(the one who helps the professor out at universities).

What's the difference between how you wrote it and how I wrote it?

I didn't join tables, but instead checked two tables for matching EmpID.

Any thoughts and/or input is appreciated, trying to figure out how all of this works.
The way you did it (two tables in the SELECT list) was deprecated, or at least highly discouraged, in the SQL-92 standard. Somehow 22 years later we still haven't managed to get rid of it. Among other disadvantages, I have to go hunting in the WHERE clause for information on how you intended the tables to be joined. This is OK for a simple query like that, but imagine listing 5 tables in the SELECT and then having like 18 WHERE clauses to look through to figure out how the joins were working. It's way more understandable to specify the join information straight away.

Your tutor should really not be accepting that answer and it kind of smacks of laziness and not wanting to teach you the right way to do things. (The presence of trailing underscores and spaces in the column names of the data sets you were given, while not "wrong", is a bit wtf).

Use of quotes to delimit column or table names in SQL is also non-standard at best. Always use square brackets.

This is how I'd write that query:

SELECT DISTINCT e.No_ EmployeeID, e.[First Name], e.[Last Name]
FROM Employee e
INNER JOIN [Employee Absence] eab ON e.No_= eab.[Employee No_]
WHERE eab.[Cause of Absence Code] = 'SICK'

A few notes about this:

- You don't need to alias column names if they're already called what you want. "e.[First Name] AS [First Name]" doesn't do anything.

- "AS" when aliasing table or column names is optional. You can just put a space in between if you like (which you have done with the tables). It's really just a style thing.

- There's no need to select both e.No_ and eab.[Employee No_] in the SELECT list, as per the join they are always identical values. (Columns don't have to be in the SELECT list to appear in ON or WHERE clauses).

Joins are easy and are usually explained badly:

INNER JOIN means "give me all the combinations of the rows in the two row sets that match the ON clause"

LEFT OUTER JOIN means "the same thing, but for any rows in the left row set which don't appear, add them to the result set with NULLs for all the right table values. I don't want to lose any rows from the left set".

RIGHT OUTER JOIN is the same thing in reverse (keep all right row set rows).

FULL OUTER JOIN adds all the rows that are added by LEFT OUTER JOIN and all the rows added by RIGHT OUTER JOIN.

That's really it. The rarely seen CROSS JOIN is just a list of all possible combinations of the two row sets. It's exactly equivalent to INNER JOIN ON 2 + 2 = 4, i.e. an ON clause that is always true.

Last edited by ChrisV; 11-08-2014 at 12:04 AM.
Help with SQL (and/or Excel) Quote
11-08-2014 , 01:17 AM
Sorry to sound like this, but the SQL you are being taught is completely unreadable, and as pointed out by ChrisV, outdated.

Something is also wrong when you can write "Might be a RDBMS database."

RDBMS simply means Relation DataBase Management System. There are a few popular ones out there (MySQL, PostgreSQL, SQL Server, Oracle, to name a few), and it is always a good idea to know which one you are using, as they all have different semantics and features.
Help with SQL (and/or Excel) Quote
11-08-2014 , 02:43 AM
Quote:
Originally Posted by ChrisV
The way you did it (two tables in the SELECT list) was deprecated, or at least highly discouraged, in the SQL-92 standard.
I just reread my post and this sentence makes no sense, should be "two tables in the FROM clause" (without a JOIN).
Help with SQL (and/or Excel) Quote

      
m