explaining an SQL query - sql

If I was given a question in an exam, where I would explain a piece of code from Microsoft Access SQL Query, without any external data or information.
How would I explain the following code in simple/plain English, I just want to get an idea because this is my first time explaining a code:
SELECT last_name, department_id, job_id
FROM employees
WHERE department_id IN
(
SELECT department_id
FROM departments
WHERE location_id = 1700
);
Is this a good explanation to a layman:
The following code: asks us to list employees' last name, their job at each department and their job identity. For each department that has a location ID equals/equivalent to '1700'. Would this be a great explanation?

SQL means Structured Query Language. Another word for "Query" is just "Question". Just about every query is written to provide a set of data that answers a question.
When you're explaining a code sample, or trying to figure out some code you inherited for that matter, a good place to start is to start by trying to figure out what question the results of the query were intended to answer. If you were sitting at your computer and somebody came up and asked you a question, what question would that be that would result in you writing that block of code? Oftentimes, just that question alone is enough of an explanation for a layperson.
Here, I'd say a fair start would be something along these lines.
This code answers the question, "What are the last names, department IDs and job IDs of everyone whose department is at location 1700?"
When giving a technical explanation, you take it to the next step. How does this code go about answering the question that was asked? This is the nuts and bolts of how the query works, as opposed to what it's supposed to do.
So, technically:
This code uses a sub-query against the departments table, which includes a location_id column, to identify all the departments at location_id = 1700. Then it uses the department list from the sub-query to get the last_name and job_id data from the employees table, using the department_id list as a filter.

As per my point of view, you need to explain the following thing
This query type is a subquery
This query provides the employee list.
This employee list is base on department and only those department need to consider which location id is 1700
So in a short way, you can say like this query give a list of employee which work on the specific location.
I hope you got this answer as correct answer.

I think a pithier explanation would be along the lines:
Fetch certain information about employees whose department is at location 1700.

Related

LIKE query with a varying number of LIKE’s

My current test revolves around a hypothetical database consisting of a PERSON table populated with their details.
I can undertake a simple LIKE query were I return the details of say a person or persons named Tom.
My question is how can I build a query were there may be multiple names and return their details?
I my exercise there is a checkbox list of names and I need to return the details for all those I select?
Thank you in advance for your assistance.

getting average value from records sql

I am writing an app that is basically a survey and I've written almost everything but this last part where I need to get stats from the survey.
The table looks like this:
user_id|question_id|answer
I need to get the average for each answer while still knowing which question ID that average value is associated with. Keep in mind that there can be multiple answers for the same question and multiple and multiple questions with the same answer. The only thing unique is the user_id. The database is Oracle but im sure I can adapt the query if needed. Just need some help with getting started.
I was able to answer my own question.
Select avg (answer), question_id
From table
Group By question_id

SQL aspx homework, stuck on doing a search advice?

Hey all first post here. I'm utterly lost on our assignment in my web database integration with c# course. My DB is a simple gradebook outlined below. The assignment is to search on an aspx page, and go to a search result page that show's in gridview the results of the DB query. We need to search for classes by department name and also a separate search page for searching by class name.
I know I have to join the tables somehow with sql, but can't recall how. I've been out sick playing catch up now that I'm back. Below is my DB outline, PK is primary KEY.
Department
DepartmentID PK - linked to Table Class 1tomany
DepartmentName
Class
ClassID PK
ClassName
DepartmentID
CreditHours
I am lost when it comes to which page gets what datasources as well, the input search page with the txtbox for the search, or the search results page that has the results? Using Visual Studio and Microsoft Access as required by the course, thank you all!
EDIT Cindy was kind enough to help me on the inner join sql I was looking for, now if I can figure out the datasources I'm golden from there. Thanks Cindy!
You actually have several questions. I can answer one of them. But honestly, you'd be better off asking your teacher or a classmate for help.
To see all the classes and their department names,
SELECT ClassID, ClassName, CreditHours, class.DepartmentID, DepartmentName
FROM Class
INNER JOIN Department ON Department.DepartmentID = Class.DepartmentID
What this does is collect all the classes and connect them to their departments, matching ON the departmentID field that's stored as part of the class row. Here's a good explanation. https://support.office.com/en-in/article/Join-tables-and-queries-3f5838bd-24a0-4832-9bc1-07061a1478f6

Getting total number of employee from multiple records

I have a timecard file that list the daily hours for each employee. I have figured out, using a query, how to get the total number of hours but I also need the total number of employees. I have searched here and on other places and tried just about everything and nothing works. I am sure it is something pretty simple but I just cannot seem to get the total number of employees. Any help will be greatly appreciated.
The main file is called Timecards and I link to a file call Employees using the field employee number to get the employee name. There are other fields in Timecards namely "Job No", "Job Description" and "Type of Hours"( such as regular and overtime) that I wanted on the summary report but I did not realize that a employee could have two entries in those fields. So that is why I could not get the total employees to come out right when using those fields.
I was finally able to create a query and print a report with the total employees using fields that only appear once in the Timecards file. So unless someone knows how to count total lines is a report I will stick with the report I have that works.
I agree with Vladimir, it helps to post what you expect to see and what kind of tables you are working with. However, I have been in the same situation. Maybe this is a good starting point in SQL
SELECT COUNT(*) AS TotalNumberEmployees FROM TheTableShowingEmployees;
Posting a question with:
A problem statement
A list of expected results
Sample Code tried
Table structures involved (Including relevant fields, data types, and tableNames)
Will get you better results. Show your work, we'll show you ours!
Given limited information so far this is the best I can come up with
I use a left join incase some of the employees haven't reported hours yet and you want all employees.
I use coalesce to change null hours to 0.
I assume that Employees Table exists and it joins to timecard on Employee_no.
I assume firstName and LastName are in the
employeetable
.
SELECT coalesce(Sum(T.hours),0) AS SumOfhours, E.FirstName, E.LastName
FROM Employees E
LEFT JOIN Timecards T
on T.Employee_no = E.Employee_no
GROUP BY E.FirstName, E.Last_name
Also note you can edit your question don't comment on it; it makes it difficult to follow.. and last but not least .... Welcome to SO!
Well after much searching and testing I finally found a post from 2009 that took care of my problem.
`If you want to count groups, you might need to add a running sum text box in
the group header. For instance, if your report contains orders and order
details but you want to count the number of unique orders, you can't use a
text box in the report footer with a control source like:
=Count(OrderID)
This expression will count orders and details.
You can add a text box in the Order header:
Name: txtOrderCount
Control Source: =1
Running Sum: Over All
Visible: No
Then add a text box in the Report Footer section:
Control Source: =txtOrderCount
Duane Hookom
Microsoft Access MVP`
Thanks for all the help.

How to find an average grade for each student with SQL

I have a list of grades that students received. Each student appears multiple times in the table.
How do I produce a list of all the students with their average grade?
P.s. I've tried looking at previously asked questions to see if I can find something relevant, but with no luck.
Look up the AVG function and the GROUP BY clause.
Since it's homework . . .
You are basically trying to aggregate data in SQL. Try googling along those lines.