Count names in alphabetical order in a table - sql

I need to count names of employees in alphabetical order from employees table in HR schema (oracle)
Example..
Last_name with. Count
A. 4
B. 3
...
...
Z. 5
How the query will look like..

I think this will do:
select SUBSTR(name,1,1) as name,count(1) as CNT from YOURTABLE
group by SUBSTR(name,1,1)
order by name

Related

Optimizing query with subselect in two tables

Table employee has two columns:
ID
NAME
Table external_job also has two columns:
ID
SALARY
I have to get one person who got the maximum salary.
The result must have three columns and one row:
ID
NAME
SALARY
I made a query but the client asked me not to use a sub-select query.
How can I do in this case?
My query is:
select *
from (select a.id,
a.name,
(select sum(salary)
from external_job b
where b.id = a.id) salary
from employee a
order by salary desc)
where rownum = 1
Use order by and some method for limiting the results to one row. In standard SQL this is:
select ej.id, e.name, ej.salary
from employee e join
external_job ej
on ej.id = e.id
order by ej.salary
fetch first 1 row only;
Not all databases support fetch first. Some use limit or select top or even more arcane constructs.
Assumption is ID is primary key on both sides.
select ID, NAME, SALARY from employee e , external_job a where e.id= a.id
order by salary desc
limit 1

SQL query where Employee works in more than one Department

I have employee table with emp id (emp_id) and department (dep_id) fields. An employee could be working in more than one Department. I want to write a sql query to display unique emp_ids who work in more than one department.
Pl help me to write sql query.
Thx
Answered here: SQL query for finding records where count > 1
You need to use count, group by and having like this.
select emp_id, count(dep_id)
from employee_department
group by emp_id
having count(dep_id)>1
Query
SELECT COUNT(*)
FROM
(
SELECT id_employee, COUNT(*) AS CNT
FROM Department_Employee
GROUP BY id_employee
) AS T
WHERE CNT > 1

What would the query be for the following sample table?

Can you please help me with a query that would display a table like this:
Dept_ID Dept_Name
10 Admin
10 Whalen
20 Sales
20 James
20 King
20 Smith
40 Marketing
40 Neena
and so on...The Schema is HR
Display the Department Id and the Department Name and then the subsequent employees last names working under that department
SELECT Dept_ID, Dept_Name
FROM Your_Table
Simple as I can make it. It's very difficult (near impossible) to tell exactly what the query should be without more detail in terms of your table structure and some sample data.
From your edit, you may need something more like this;
SELECT DT.Dept_ID, DT.Dept_Name, ET.Emp_Name
FROM Dept_Table AS DT INNER JOIN Emp_Table AS ET ON DT.Dept_ID = ET.Dept_ID
ORDER BY Dept_ID
This shows the employees in each department on the next column, you don't really want all that in the same column.
When you union two data sets, there is NO implicit ordering, you could get the results in any order.
The get a particular order you must use ORDER BY.
To use ORDER BY, then you must have fields to do that ordering by.
In your case, the pseudo code would be...
- ORDER BY [dept_id], [depts-then-employees], [dept_name]
The middle of those three is something that YOU are going to have to create.
One way of doing that is as follows.
note: Just because you have a field to order by, does not mean that you have to select it.
SELECT
dept_id,
dept_name
FROM
(
SELECT
d.dept_id,
d.dept_name,
0 AS entity_type_ordinal
FROM
department d
UNION ALL
SELECT
d.dept_id,
e.employee_name,
1 AS entity_type_ordinal
FROM
department d
INNER JOIN
employee e
ON e.dept_id = d.dept_id
)
dept_and_emp
ORDER BY
dept_id,
entity_type_ordinal,
dept_name
Assuming there's a table in your database called departments that holds this information, your code might look like this:
select
dept_id, dept_name
from
departments
If you want to display certain columns of the table like you have asked in the question above , you can use the following syntax :
select column_names from table_name
replace:
column_names with the column names you want to display separated by a coma
2.table_name with the name of the table whose columns you wish to display
for the above question , the following code will do:
select Dept_Id , Dept_Name from Department ;
The above code works if your table name is 'Department'

sql command to display highest repeated field in a column

How to display the highest repeated field in a column in sql ?
for eg if a column contains:
jack
jack
john
john
john
how to display the maximum repeated field (i.e) john from the above column?
select chairman
from mytable
group by chairman
HAVING COUNT(*) = (
select TOP 1 COUNT(*)
from mytable
group by chairman
ORDER BY COUNT(*) DESC)
select name from persons
group by name
having count(*) = (
select count(*) from persons
group by name
order by count(*) desc
limit 1)

Need a hand with a simple query

I need a help with a query. I think is not so difficult.
I need to do a select with distinct and at the same time, do a count(*) of how many rows are returned by this distinct.
One example:
Table names>
Id Name
1 john
2 john
3 mary
I need a query thats return:
Name Total
john 2
mary 1
select name, count(*) from names group by name;
SELECT name, COUNT(*) FROM names GROUP BY name
SELECT name, count(*) as occurrences FROM names GROUP BY name