A new government reporting regulation requires you to develop a query that can count the number of male dependents of employees of the company. The information is stored in the dep_gender column of the dependent table. The result table should have a single output column labeled Number Male Dependents.
So for this part I have :
SELECT COUNT(DEP_GENDER)"Number of Male Dependents"
FROM dependent
where dep_gender = 'M';
which works just fine now to 'revise' for the next part.. I am having some problems because I cannot figure out how to filter into two different groups with associated counts for males and females.
A revision to the government reporting regulation requires the report to count the number of male and female dependents of the company. Display the information as two columns, one for gender and one for the associated count. The result table should have two rows, one for each gender. Use a single query. Additionally, the gender output column should be formatted as CHAR(6) and have a heading label of Gender. The count column should have a heading label of Number Counted.
I think you're looking for a Group By query:
Select Cast(Dep_Gender as Char(6)) as Gender,
Count(1) 'Number Counted'
From Dependent
Group By Cast(Dep_Gender as Char(6))
Cast is used since the requirements (homework perhaps) are to format the gender as a char(6).
SELECT 'Male' As [Gender], COUNT(DEP_GENDER) AS [Counted]
FROM dependent
where dep_gender = 'M'
UNION ALL
SELECT 'Female', COUNT(DEP_GENDER)
FROM dependent
where dep_gender = 'F'
Related
I was trying to solve the problem of "The number of people of each gender there are in the database, where the gender is known, display the count of people against each gender" in SQLite. I typed a query
SELECT sex, COUNT(name) from employee_table GROUP BY sex ORDER BY sex DESC;
which is partially correct as it showed as output the number of males and females. But, it also shows the number of blank entries which were neither classified as Male or Female.
I have to stop displaying the count of the blank entries. I guess for this, I have to use a CASE statement. I tried CASE statement in different ways. But those didn't work. I tried searching the internet and stack overflow and tried those methods. Those also didn't work.
Any help would be appreciated.
Thanks,
Richard
You want to filter the results and a case expression doesn't do that. Without sample data, it is difficult to say exactly what the expression is, but perhaps:
SELECT sex, COUNT(name)
FROM employee_table
WHERE sex IS NOT NULL
GROUP BY sex
ORDER BY sex DESC;
If the "blank" results are really empty strings, then that would be:
WHERE sex <> ''
This also filters out NULL values.
Or if you want explicit values, then you can use IN:
WHERE sex IN ('m', 'f')
Apologies for my simple problem, I am an absolute novice. I have the following code in separate queries
I am attempting to display 3 columns, the average male salary for a set job, average female salary for a set job and the JobID. Separately these queries work however I cannot work out how to combine them.
I have tried multiple solutions from this site for example trying to put multiple select statements inside
and also by using a 'union' solution however cannot get either to work.union This simply compiles them into a single column and sorts via salary not JobID.
SELECT Round(Avg(Salary)) AS AverageMaleSalary, JobID
FROM Employee WHERE Gender = "M"
GROUP BY JobID;
SELECT Round(Avg(Salary)) AS AverageFemaleSalary, JobID
FROM Employee WHERE Gender = "F"
GROUP BY JobID;
You could use conditional aggregation
SELECT JobId,ROUND(AVG(IIF(Gender='F', Salary, NULL))) AS AverageFemaleSalary
,ROUND(AVG(IIF(Gender='M', Salary, NULL))) AS AverageMaleSalary
FROM Employee
GROUP BY JobId;
A company wants to know what has happened since the beginning of 1999 on hiring. The human resources manager has asked you to
produce a count of the employees hired since then, broken down by both age and gender simultaneously (i.e. 17 males, 25 females, etc.). Write a query that does that.
This is what I have so far. I couldn't figure out how to merge two columns simultaneously. Any thoughts?
SELECT EmployeeID, COUNT(*) AS "Number of employees"
FROM Employee
WHERE Age and Gender
GROUP BY EmployeeID
HAVING COUNT(*) BEGIN = 1999
I am not pretty much sure about your requirement but the right way to write a query is as following and this may fulfill your requirement with slight changes...
SELECT Gender, COUNT(*) AS "Number of employees", Age
FROM Employee
WHERE year(column_name) >= 1999 --Give a real date column here
GROUP BY Age, Gender
Please provide table structure and complete desired output for the exact answer.
I am kind of new to SQL and I made a couple of tables to practice. The columns may have some unrelated categories but I don't know what else write...
Anyway, basically what i want to do is get info from two tables based on the first and last name from one table.
Here are my tables:
Order
Host
I want create a query to pull the ticket number, height, order, subtotal and total by first and last name. The only orders I want to pull are from John Smith And Sam Ting. So in the end, I want my extraction to have the following columns:
Ticket Number
First Name
Last Name
Height
Order
Subtotal
Total
Any help or direction would be awesome!
With the assumption the tables both have unique Ticket_Numbers and that will provide a one-to-one mapping between then.
SELECT
Order.Ticket_Number,
First_Name,
Last_Name,
Height,
Order,
Subtotal,
Total
FROM Order
JOIN Host on Host.Ticket_Number = Order.Ticket_Number
WHERE
(First_Name = 'John' AND Last_Name = 'Smith')
OR (First_Name = 'Sam' AND Last_Name = 'Ting')
You need to "call" the table name first, and then the column. After that you need to use the "join" for the 2 tables. And finally you need the "where". I didn't look for the details so you need to check the "names".
SELECT Order.Ticket_Number, Order.First_Name, Order.Last_Name, Order.Height, Order.Order, Cost.Subtotal, Cost.Total
FROM Order
INNER JOIN Cost
where First_Name="Jhon" and Last_Name="blablabla"
or
First_Name="SecondGuy" and Last_Name="blablabla"
I just started learning SQL a couple of days ago, and I'm trying to understand which order I should use the SELECT statement when building subqueries. Here is a simple example of what I am trying to accomplish.
I have two tables - One that specifies the demographics of a customer list, and the other that details how they heard about our company.
In the first table, my customers are listed as either Male or Female in the Gender column, and then in the next column, their ethnicity is specified (Caucasian, African American, Hispanic, etc).
In the second table, there is a Referral column that specifies how they heard about our company (TV, radio, website, etc).
I want to first filter the customers by gender (I want to show only Female data), and then I want to count how many times our customers found us through our website for each ethnicity listed in the table.
SELECT Ethnicity, COUNT(Referral)
FROM Demographics, Marketing
WHERE Demographics.id = Marketing.source_id
AND Referral = 'website'
/* confused about how to put subquery here saying Gender = 'Female' */
ORDER BY Ethnicity
Basically, I'm confused about how to properly include the subquery and if I am even filtering in the correct order.
But here is what I want my table to look like:
/*Data is shown for ONLY Females */
Referral Caucasian African American Hispanic Asian
website 7 19 14 22
I'm sorry, this code is probably really messed up. Please help if you can.
From what you've described, you don't need a subquery:
SELECT Ethnicity, COUNT(Referral)
FROM Demographics, Marketing
WHERE Demographics.id = Marketing.source_id
AND Referral = 'website'
AND Gender = 'Female'
ORDER BY Ethnicity
...note that this gives you a different resultset from the one you've shown, though, with ethnicity, count on each row.
When you are using two table u must use the INNER JOIN syntax:
http://w3schools.com/sql/sql_join_inner.asp
SELECT Ethnicity, COUNT(Referral)
FROM Demographics INNER JOIN Marketing
...
AND Referral = 'website'
AND Gender = 'Female'
...