LIMIT on SQL query - sql

How can I LIMIT the results to 10 in the following query?
I use SQLSRV.
SELECT Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?

Use TOP:
SELECT TOP 10 Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?

use
select top(10) Id, Firstname, Surname ....

The answer by kevingessner is certainly the easiest way.
I just thought I would throw some alternatives in for fun.
SET ROWCOUNT 10
SELECT Id, Firstname, Surname FROM Person WHERE Firstname LIKE ?
SET ROWCOUNT 0
Or a more convoluted way:
With q
as
(
Select ROW_NUMBER() Over(Order by Id) as rn,
Id,
Firstname,
Surname
FROM Person WHERE Firstname LIKE ?
)
Select *
From q
where q.rn <= 10

Related

show extra columns where group has count of 1

Suppose I have a table Students with just 2 columns LastName and FirstName. I know I can get all the LastNames that only have a 1 FirstName with:
select LastName from Students group by LastName having count(*) = 1
But what if I also want to show the FirstName for those rows?
You could filter with a correlated subquery:
select s.*
from students s
where (select count(*) from students s1 where s1.LastName = s.LastName) = 1
Or, if you have a primary column, you can use not exists:
select s.*
from student s
where not exists (
select 1 from students s1 where s1.LastName = s.LastName and s1.id <> s.id
)
This query would take advantage of an index on (id, LastName).
Finally, another common option is to do a window count:
select *
from (select s.*, count(*) over(partition by LastName) cnt from students s) t
where cnt = 1
You could also do
Select * from Students where lastname in (Select lastname from Students group by lastname having count(*) =1)
Just add it to the select:
select s.LastName, min(s.firstname)
from Students s
group by s.LastName
having count(*) = 1;
If only one row matches, then min() returns the value on that row.
You need to get it using any aggregate function that can return the desired value. Min, Max, first_value(), ir string_agg() you may want all names with the lastname in other use case
select LastName,
first_value(firstname) over (order by firstname) firstname,
Min(firstname)
from Students
group by LastName having count(*) = 1
I found another one myself:
with LastNames as (
select LastName from Students group by LastName having count(*) = 1
)
select LastName, FirstName from Students
where LastName in (select LastName from LastNames)

SQL Union Select for alternate data

I created an sql query with union select and here is the query to join the two columns into one.
(select top 10 FirstName from Users) union (select top 10 LastName from Users)
Here is the Result:
QUERY RESULT 1
And here is the original data for the result 1 of union select.
ORIGINAL DATA
So, here is my problem.
How do I select the data of each firstname and lastname with the same column but the first one is firstname and the second one is lastname. For example:
Tumbaga Temp - <FirstName>
Villamor - <LastName>
Jun - <FirstName>
Villamor - <LastName>
FN83 - <FirstName>
Lising Geron - <LastName>
So on and so fort.
I am new in sql query. Thanks for your help.
We add a common row_number() to both parts to essentially group them, then order by this and the name type to display in clusters of first/last pairs
select 'First' as thename,
Firstname,
row_number() over(order by firstname) rn
from Users
union all
select 'Last',
Lastname,
row_number() over(order by firstname)
from users
order by rn, thename
If you only want the 1st 10, then wrap this and add a clause
select *
from
(
select 'First' as thename,
Firstname,
row_number() over(order by firstname) rn
from Users
union all
select 'Last',
Lastname,
row_number() over(order by firstname)
from users
)
where rn <=10
order by rn, thename
No need to use union, As per your description You have two columns 'firstName' and 'lastName' in a table and you want both in a single column. Just try the following query-:
select FirstName+' '+LastName as FullName from Users
SQL Server
you can add a column to both queries with your favourite data to be selected.
(select top 10 FirstName, 'FirstName' as NameType from SysUser) union (select top 10 LastName, 'LastName' as NameType from SysUser)

SQL add IDENTITY base on grouped column value

I have data like this:
FirstName LastName
El Even
Mike Wheeler
Mike Byers
Dustin Henderson
And my desired output is to add Identity into each of the unique FirstName
ID FirstName LastName
1 El Even
2 Mike Wheeler
2 Mike Byers
3 Dustin Henderson
The way I do this is:
/* part 1 */
SELECT IDENTITY(int, 1,1) AS ID, FirstName
INTO TabTemp FROM TabName
GROUP BY FirstName
/* part 2 */
SELECT B.ID, A.FirstName, A.LastName
INTO TabTempFinal FROM TabName A, TabTemp B
WHERE A.FistName = B.FirstName
My question is can I achieve the result without using the part 2?
You could just use the dense_rank window function:
SELECT DENSE_RANK() OVER (ORDER BY first_name) AS id,
first_name,
last_name
FROM tabname
Not really. You can't add an identity column to a table where the identity is not supposed to be unique. That is not how identity columns work.
But, you can use dense_rank() instead:
SELECT DENSE_RANK() OVER (ORDER BY FirstName) AS ID,
FirstName, LastName
INTO TabTempFinal
FROM TabName ;
This generates the unique indicator for each FirstName. The only difference is that the column is not an identity column.
You could do an update with a CTE using DENSE_RANK:
WITH cte AS (
SELECT FirstName, LastName, DENSE_RANK() OVER (ORDER BY FirstName) dr
FROM TabName
)
UPDATE cte
SET ID = dr;
This assumes that you already have a column called ID.

Coun(*),distinct and column

How to accomplish like this in sqlserver
select firstname,count(*) from
(select distinct firstname,lastname,amount
from emp)
group by firstname
This works in oracle.
I don't understand what are you trying to do, but your query could be written this way:
SELECT firstname, COUNT(*)
FROM emp
GROUP BY firstname;
Or:
select firstname, count(*)
from
(
select distinct firstname, lastname, amount
from emp
) AS t
group by firstname;
You can also use the DISTINCT keyword inside the COUNT; something like this: COUNT(DISTINCT columnanme) instead.
Or:
select firstname, lastname, amount, COUNT(*)
from emp
group by firstname, lastname, amount;
Here's an example from some stuff where I work that does basically the same thing:
SELECT SYS_CSL, COUNT(SYS_CSL)
FROM (
SELECT SYS_CSL
FROM [VANTAGE_READ].CSL_SERV_RANK
WHERE AGNT_CSL = 0
AND (STOP_DTE_CSL > GETDATE() OR STOP_DTE_CSL IS NULL)
) t1 GROUP BY sys_csl
You need to name the derived table
select firstname, count(*)
from (select distinct firstname,lastname,amount from emp) derived
group by firstname

How we can use CTE in subquery in sql server?

How we can use a CTE in a subquery in SQL Server?
like:
SELECT id (I want to use CTE here), name FROM table_name
Just define your CTE on top and access it in the subquery?
WITH YourCTE(blubb) AS
(
SELECT 'Blubb'
)
SELECT id,
(SELECT blubb FROM YourCTE),
name
FROM table_name
It doesn't work:
select id (I want to use CTE here), name from table_name
It's not possible to use CTE in sub queries.
You can realize it as a work around:
CREATE VIEW MyCTEView AS ..here comes your CTE-Statement.
Then you are able to do this:
select id (select id from MyCTEView), name from table_name
Create a view with CTE/ Multiple CTEs with UNION sets of all CTEs
CREATE VIEW [dbo].[_vEmployees]
AS
WITH
TEST_CTE(EmployeeID, FirstName, LastName, City, Country)
AS (
SELECT EmployeeID, FirstName, LastName, City, Country FROM Employees WHERE EmployeeID = 4
),
TEST_CTE2
AS (
SELECT EmployeeID, FirstName, LastName, City, Country FROM Employees WHERE EmployeeID = 7
)
SELECT EmployeeID, FirstName, LastName, City, Country FROM TEST_CTE UNION SELECT * FROM TEST_CTE2
GO
Now, use it into sub query
SELECT * FROM Employees WHERE EmployeeID IN (SELECT EmployeeID FROM _vEmployees)