Most voted "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
11
votes3
answers1559
viewsHow to condition the insertion of a record in SQL Server?
How can I condition the insertion of a record in a table in SQL Server using the T-SQL language considering the following rules: If the record to be inserted already exists in the table, only update…
-
11
votes5
answers64148
viewsDifference between two dates in days, hours, minutes and seconds
Problem: How to subtract between two dates and return the value in Days, Hours, Minutes and Seconds in an SQL query with SQL Server 2014? Example: Dados: DataRecebimento DataEnvio 2013-11-29…
-
11
votes3
answers175
viewsWhat is the most correct way to make a query with LINQ?
I am trying to make a LINQ query in my BD SQL Server, but the result of it always comes 'null'. I need the first user ID I query. This is the way I try to consult: public decimal…
-
11
votes4
answers16866
viewsHow to know which DATETIME format is used in a given column of SQL Server?
I have a table on SQL Server and one of the camps was created as DATETIME. How should I know query or SQL Server Studio, what format default used of this DATETIME in my SQL Server?…
-
11
votes2
answers32556
viewsHow to remove duplicate lines efficiently?
I’m normalizing a table here and found that there are duplicate lines. The way I chose to clear these lines is through the: CREATE TABLE tabela_nova AS ( SELECT DISTINCT * FROM tabela_antiga ); The…
-
11
votes2
answers14208
viewsWhen to use WITH (NOLOCK)?
Select * from MinhaTabela with (NOLOCK) I know colleagues who always insert the clause With (NOLOCK) in their selects and I know others who preach that NOLOCK is bad practice and should never be…
-
11
votes1
answer3203
viewsLeft Join or Not Exists
I needed to retrieve information that was in a certain table, but were not in another one. Searching found that NOT EXISTS would serve this case, but I saw that Left Join brings the same result: NOT…
-
11
votes2
answers10914
viewsData type for latitude and longitude
I am working on a new project where I will need to save the coordinates (latitude and longitude) of an address in the database. There is an ideal data type for this? I searched and saw some decimal…
-
10
votes5
answers2566
viewsIs it recommended to use Linq instead of SQL?
To avoid string abuse and avoid code injection problems can be a good practice using the Linq. I can also imagine, for example, that it’s slower. Who ever used the Linq to communicate with the…
-
10
votes3
answers3229
viewsFind certain dates in VARCHAR field in SQL (Mysql)
I have the following condition: WHERE sv_users.userDataNascimento LIKE '%95%'; And this condition searches a date (11/12/1995), of the kind VARCHAR and not Timestamp, one year ending with the…
-
10
votes4
answers16962
viewsWhat do you call a "Stored Procedure"?
How to call a procedure in ASP.NET C#? Follow my simple procedure: CREATE PROCEDURE GetAdmin ( @email VARCHAR(50), @password VARCHAR (50) ) AS BEGIN SET NOCOUNT ON; SELECT * FROM Admin WHERE Email =…
-
10
votes3
answers9148
viewsQuery in two tables without INNER JOIN
I would like to know, is there a difference in the performance of these two queries? in Mysql SELECT employee.*, company.name FROM company, employee WHERE employee.company_id = company.id AND…
-
10
votes4
answers2357
viewsGood practices for storing logs
What information should be stored? It’s good practice to use TRIGGER in the database for storing logs or via code is something safer and easy to maintain?…
-
10
votes5
answers26385
viewsWhat is the best practice to know if an Row exists in a SELECT in Mysql?
For example, when we check if a user is already registered in the table, we do not need any data return by query, just check if the number of Rows is greater than 0 (num_rows > 0). Us scripts and…
-
10
votes1
answer5591
viewsHow to make SQL query that ignores uppercase, lowercase and accents?
I have a database with registration of several films, and I would like to create an SQL query that ignores the presence of capital letters, lowercase, accents and ç. For example when searching for…
-
10
votes4
answers3043
viewsWhat are temporary tables for?
What use can these so-called temporary tables offer? Is there any advantage to using them? There is a difference between the temporary tables and the "normal tables"?
-
10
votes2
answers8715
viewsWhat is the use of using foreign keys?
I’ve been learning about foreign keys and I’m having a hard time understanding what they’re useful for. Its purpose would be to "divide a table into several tables, leaving its data always atomic",…
-
10
votes2
answers6254
viewsWhat is data persistence?
What would that term be? I always see around and can’t decipher!
-
10
votes3
answers1808
viewsSecurity when creating a PDO query function
I am now migrating to PDO because of security, but I don’t know if I am using PDO correctly. I am creating these functions to make faster the use of PDO, but I do not know if this form is safe. It’s…
-
10
votes2
answers5991
viewsBIT(1) versus TINYINT(1) for boolean values
I may be wrong, but I have the impression that in practice the default in Mysql is to use columns of the type TINYINT(1) to store boolean values, true/false or 0/1. Only that TINYINT accommodates up…
-
10
votes2
answers49607
viewsHow to format a Datetime field in Brazilian dd/MM/yyyy format?
I would like to format the fields DataAfr and DataTrm of the kind DATETIME, in the Brazilian date format dd/MM/YYYY instead of the American format yyyy-mm-dd hh:mm:ss SELECT e.NmrCnt AS [Contrato]…
-
10
votes1
answer1031
viewsSort Varchar field by taking into account groups and subgroups
I need to sort the values below in ascending order. They are Varchar type and the solutions I have found so far for Mysql have not worked. An example of the data type would be 1.12.02, because these…
-
10
votes3
answers8992
viewsSelect first record within a segmentation in SQL Server
I have the following example table: The consultation asks me to show which students entered first in each course. I can tell which student entered the university first, using the function top(1),…
-
10
votes2
answers1392
viewsWhat are they, Readpast and Nolock?
What is READPAST and NOLOCK? I’ve seen quite a lot of the use of NOLOCK, but the READPAST I saw the use now, practically the same way, ie. FROM dbo.table t WITH(READPAST) and FROM dbo.table t…
-
10
votes2
answers3515
viewsWhat is the difference of an empty string and NULL in SQL?
What’s the difference of storing a string as NULL or empty in SQL? How these two can behave when I go to make one SELECT, or INSERT worthwhile '' in that column which is of the type varchar? If I…
-
10
votes5
answers972
viewsHow to get the name of the day of the week on the first day of the month?
How do I get the name (Monday, Tuesday, Wednesday...) of the first day of the month of December from the current date?
-
10
votes2
answers413
viewsUpdate with Case When you need Where?
UPDATE bethadba.FOFILHOS SET CPF = CASE WHEN CODI_EMP = 61 AND I_EMPREGADOS = 156 AND i_filhos = 1 THEN '00551366133' WHEN CODI_EMP = 57 AND I_EMPREGADOS = 290 AND i_filhos = 1 THEN '00636170993'…
-
10
votes1
answer272
viewsComparison: CTE vs CURSOR
I do not know if this is a question that many ask, but I have always had it in mind almost always. Usually, when I need to deliver a script corrective to a customer, used only to be executed once, I…
-
9
votes1
answer584
viewsDifference in Indice Unique and Unique Constraint usage in Informix?
I would like to know the behavior/differences between a Indice Unique and Unique Constraints to the Informix ?
-
9
votes3
answers2825
viewsCheck whether a date and time (timestamp) represents the current day
I have a table with the following field: dh_send (TIMESTAMP) and I want to mount a query to get all records of this table in which the dh_send is equal to the current date (TODAY). I thought as…
-
9
votes2
answers33949
viewsHow to catch the current year on ORACLE?
I need to get the annual year on ORACLE, but I only know how to get the current date, so: SELECT SYSDATE FROM DUAL You can manipulate it to get only the Year?
-
9
votes2
answers3276
viewsHow to make a Stored Procedure dynamic by taking data from other tables in the database?
I’m on a project to build a social network with the @Rodrigoborth, and we have the problem of How to index and update a user comparison system... We were given the idea of working with Stored…
-
9
votes1
answer58526
viewsHow do I make a SELECT in 2 or more Tables with 2 or more conditions?
I have a problem making a select with the following conditions: I want to show the data either with condition1 or with condition2 Select * tabela inner join tabela2 WHERE campo = 0 and campo2 = ''…
-
9
votes3
answers25430
viewsSQL command to display specific records from the "id" field
Which SQL command would display the records where the field id were 22, 23, 25 and 27?
-
9
votes1
answer1625
views -
9
votes1
answer5672
viewsLeft Join with lambda Groupjoin and with condition
How can I make one left join with some conditions using lambda LINQ expressions? In SQL Server have this query: select usr.Id, usr.FirstName, usr.LastName, ex.Id from User usr left join Exam ex on…
-
9
votes4
answers114
viewsWhy is this SQL statement incorrect?
SELECT * FROM produtos WHERE categoria = 'Vestidos' AND cor = 'Branco' OR cor = 'Rosa' Returned 3 records, 2 of them from the category Dresses, and 1 of the category Shoes. Why not returned only…
-
9
votes3
answers14735
viewsHow to break a line of a Mysql field into an HTML page
I need to save a value with a line break in the table For example, instead of keeping: 0 anos, 3 dias Keep: 0 Anos 3 Dias I tried the following process: INSERT INTO tabela (tempo) VALUES ('0 Anos\n3…
-
9
votes2
answers295
views -
9
votes2
answers144
viewsIs it redundant to use LIMIT in a QUERY whose ID is the primary key?
My question is if using LIMIT there will be some performance gain in the QUERY. Example: SELECT descricao FROM produto WHERE id = 9999 LIMIT 1 Has better performance than? SELECT descricao FROM…
-
9
votes6
answers49720
viewsConvert DATA dd/mm/yyyy hh:mm:ss to yyyy/mm/dd
I have a date in the following format 14/12/2015 00:00:00 and need to convert to 2015-12-14 as I can do it on SQL?
-
9
votes2
answers5074
viewsWhat is the use of 'SELECT 1'?
I am making a query using NOT EXISTS on a college job. The situation is as follows: 1.14 - Designing the CPF and the amount to pay in rentals by customers who have rented media and have not made any…
-
9
votes3
answers1244
viewsHow to make a NOT IN SQL?
I want to get all pilots who have never performed flights on route 12345, I have tried with NOT IN and so on: SELECT pl.id FROM Piloto pl, Voo v, Rota r WHERE pl.id = v.id_comandante AND pl.id =…
-
9
votes3
answers830
viewsConvert rows to columns (Pivot?)
I have a chart with 33 million phone records with the structure below: ccpf_cnpj ddd telefone tipo origem 11111111111 83 81021111 M SERASA 11111111111 83 87472222 M SERASA 11111111111 83 81023333 M…
-
9
votes2
answers1340
viewsSmart Query with Mysql
I’m trying to make a "smarter" query on my DB, my doubt is as? I have a query: SELECT * from publicacao where titulo like '%$busca%'; and in my DB has several publishing titles, for example:…
-
9
votes1
answer3234
viewsWhat is dynamic SQL?
I have seen many cases where some show how to use, and others where they simply advise against using SQL dynamic. I had the following doubts: But anyway, when we should or should not use dynamic…
-
9
votes4
answers785
viewsShould I use a "Try-catch" to identify if a password is wrong?
On the screen of login, carry out the verification in the bank by means of a select, and I’m using the catch to capture this exception. It is correct to use the catch for that guy? if (Usuario !=…
-
9
votes2
answers34544
viewsHow to get column names from a table in SQL Server?
Table Languages: How to select the Column Names of this table? The result would be: "Ididiome", "Acronym", and "Language".…
-
9
votes1
answer517
viewsWhat is the name given for the number of columns in a table?
What is the name given for the number of columns in a table? And the name for the number of lines?
-
9
votes3
answers3392
views_ (underline) in consultation with LIKE
I’m making a query where I need to return all information from a given table, which contains "_R_" in the nomenclature. However, when using WHERE NM_CAMPAIGN LIKE '%_R_%' he returns me to other…