Sql server面试问题 [英] Sql server interview question

查看:89
本文介绍了Sql server面试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have attended an interview recently, and the interviewer asked me this question:

UserId  UserName
1          Name1
1          Name2
2          Name3
Here he wants me to retrieve either Name1 or Name2 using where condition?

How can I get the result?

I wrote like

select Username from Users where Username='Name1' or Username='Name2' 

but here both the conditions are satisfied so two records are coming... What will be the query to retrive the data?





我尝试了什么:





What I have tried:

select Username from Users where Username='Name1' or Username='Name2' 

推荐答案

hareesh babu写道:
hareesh babu wrote:

UserId用户名

< span class =code-digit> 1 名称1

1 名称2

2 Name3

UserId UserName
1 Name1
1 Name2
2 Name3



我想我会指出系统中存在不匹配。不应该有两个具有相同用户ID的用户,因此主键丢失/配置错误或者系统中存在一些灾难性故障。



如果密钥是就地(1,2或3),那么最好的选择可能是在WHERE子句中使用密钥。


I think I would have pointed out that there is a mismatch in the system. Two users having the same user ID shouldn't be possible so either the primary key is missing / configured in a wrong way or there is some catastrophic failure in the system.

If the keys would be in-place (1, 2, or 3) then probably the best option would be to use the key in the WHERE clause.


这篇关于Sql server面试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆