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
-
6
votes2
answers461
viewsHow to sort the results of a ranking?
I am developing a game, and in it I need to make a ranking according to the time it took the user to reach the end of the game. My question is the following: what kind of data should I use in my…
-
6
votes3
answers17415
viewsMysql field type change
I wonder if it is possible to change the data type of a column in Mysql. For example: I have a table USUARIO with the column SITUACAO, in varchar and need to change to bit. I thought I’d do…
-
6
votes4
answers34297
viewsTake the highest value per category by also loading the ID
The table is in the format below ID | CATEGORIA | QUANTIDADE 1 | A | 23 2 | A | 12 3 | A | 11 1 | B | 15 2 | B | 10 Wanted a query to return to me the following table ID | CATEGORIA | QUANTIDADE 1 |…
-
6
votes1
answer742
viewsImprove the performance of a query
How could I improve this query, which returns me the top 10 sales to assemble a chart, so that it processes more quickly? set @startDate :='2015-01-03'; set @endDate :='2015-05-31'; set @dst_id:=1;…
-
6
votes1
answer2994
viewsModel "Product" table for multiple different product types
How to model the Table Product from a data bank? This table should store the following information: id, product name, quantity, price, status (if it is in stock, if it will be returned) and…
-
6
votes1
answer1113
viewsSet default value for database column in Laravel 4
How can I add a default value to a column of my Mysql table via Laravel 4? In SQL would be: create table tabelaTeste( id int NOT NULL AUTO_INCREMENT, coluna1 varchar(50) DEFAULT valor, PRIMARY…
-
6
votes4
answers11049
viewsHow to select in 3 tables?
I need to do SELECT in two tables with relationship n for n, soon, I will have to make use of the intermediate table as well, but I don’t know how to do it. I am using PHP and Sqlserver database.…
-
6
votes1
answer2951
viewsLEFT JOIN using Linq
I have the following tables: Responsavel | Filho Id Nome | Id Nome ResponsavelId 1 Ana | 1 Aninha 1 2 Maria | 2 Ana Júlia 1 3 Pedro | 3 Mariazinha 2 I’d like to make a SELECT with INNER JOIN where…
-
6
votes2
answers1494
viewsDelete record with duplicate (Id) leaving only one occurrence
This is an old bank, dbase who was transferred to MSSQL. How to leave only one occurrence in the case of id duplicated in a table? | Id| Nome | |-----------| | 1 | JOSE | | 1 | JOSE | | 2 | MARIA |…
-
6
votes4
answers268
viewsBETWEEN demands arguments in ascending order? Why?
I have the following dummy table. I created the following query: SELECT * FROM ALUNOS WHERE IDADE BETWEEN 10 AND 15 Students A, C and D return. Now, because when I invert the order in the BETWEEN…
-
6
votes1
answer266
viewsHow to create a scan sequence in the Postgres database?
I have the following table in the database Postgres: CREATE TABLE customer ( userid character varying(30) NOT NULL, firstname character varying(30) NOT NULL, lastname character varying(30) NOT NULL,…
-
6
votes3
answers280
viewsHow to mount SELECT?
I have a Products table with: ProCodigo - Primary Key de Produto ProNome And another Price History table with: HisCodigo - Primary Key de Histórico HisData HisPreco ProCodigo - Foreign Key de…
-
6
votes1
answer439
viewsHow to use Prepared statements with external variables in Mysqli
I have the following code, which I want to use prepared statments: prepare.php: <?php include "../conex.php"; // conecta mysqli_set_charset($mysqli,"utf8"); // Transforma em UTF8 pra gravar…
-
6
votes2
answers188
viewsSQL Query Optimization in Mysql and Index
Guys I’m having a performance problem at a consultation SQL in the MySQL who is using my server a lot, I’ve done index and yet consumption does not decrease. The query I’m using is: SELECT CONCAT(…
-
6
votes1
answer890
viewsHow to maintain multiple connections with postgresql in Node.js?
I am used Node.js in a task where I need to migrate data between two Postgresql databases. The idea is more or less the following: Connect to Bank A. Connect to Bank B. Return all records from A.…
-
6
votes4
answers5546
viewsSQL query to get all records from last month but until the current day
Good morning, I need to know a percent variation of last month’s job records for the task logs you did this month. For this I need to compare with the current day, for example. If today were…
-
6
votes1
answer23089
viewsHow to pass excel data to Mysql?
First, I have a newsletter system that I modified for email marketing, it’s all in PHP (obvious) and first, I add the emails manually through a part of the site add.php but there are over 3000…
-
6
votes1
answer149
viewsWhy does OFF SET slow down SQL?
I have the following SQL, generated by Entity Framework 6. SELECT [Project1].[C1] AS [C1], [Project1].[ID] AS [ID], [Project1].[CAMPO1] AS [CAMPO1], [Project1].[CAMPO2] AS [CAMPO2],…
-
6
votes1
answer462
viewsWhat is the difference between the Mysql Trigger and Mysql Event exception?
I would like to create a e-mail trigger scheduler, without the use of cron, and would like to know if it is possible to do this through Mysql. Searching the Internet I saw some examples of event…
-
6
votes3
answers719
viewsHow to get tickets that were open in May?
I’m working with a database that contains all the tickets registered in the otrs of the company where I work. I intend to do a query where I return all tickets that were open in May. The problem is…
-
6
votes1
answer1911
viewsMaximum size for database tables
What can be the maximum size that a table can have? To make it clearer: a table in Mysql, what is the maximum size it supports? And in SQL Server, Oracle, Postgresql?
-
6
votes3
answers604
viewsWorking with tags in Mysql
In a system I would like to categorize some users with tags. However there are groups of tags (for example, UF tag group, Interests). I’m trying to do a query where I can answer some questions like:…
-
6
votes2
answers402
viewsPass ROWTYPE parameter with the EXECUTE command
I am developing a function in Postgres that aims to recover for each record of a query the result value of a check contained in a set of functions. Of these functions only one will return the…
-
6
votes1
answer1328
viewsHow to restrict dates in Mysql and SQL Server?
I wonder if I can restrict certain dates in the database itself, not in the code. For example, I have a table that records date of entry and date of exit. Here are some requirements. The date of…
-
6
votes2
answers737
viewsConcatenate fields in a more performative way
I am concatenating the description lines of a record so that instead of having N records for a description it has only one. I can make it happen, but I wanted a more performative way, because I’m…
-
6
votes2
answers21084
viewsBrazilian Time Zone in Mysql
I have a Mysql database table called registros. In it, I then have the following structure: id_registro, nome_registro, data_registro. In data_registro i am using the current_timestamp(), searching…
-
6
votes2
answers9412
viewsIs it possible to make an INSERT INTO SELECT + other values outside of SELECT?
I need to insert into a table ACCESS some values from another table plus the time and the user who made the record. I know to get the data from the other table just do something like: INSERT INTO…
-
6
votes3
answers1926
viewsHow do you know how many days are in the current month?
How to know how many days is the current month Mysql, example: setembro = 30dias Motive: I have a goal : exemplo: 1.000,000 I need to take the value of the goal and divide by the amount of days you…
-
6
votes3
answers23959
viewsDisable SQL Server Column Identity
How do I disable the property identity of a column in the SQL Server? I’ve tried to: SET IDENTITY_INSERT ON/OFF But it didn’t work. I don’t know if it’s only for insertion, but I need to do an…
-
6
votes1
answer4952
viewsHow to update a datagridview automatically?
I created an application that will display the data of a particular view in a control datagridview. The entire application is already ready: the data upload, the update button and also the events…
-
6
votes3
answers324
views"Translate" query from Mysql to SQL Server 2012
What is the SQL Server 2012 query that corresponds to the next Mysql query? REPLACE INTO schedule SET jobname = "sqldump" , last_exec_date = NOW()…
-
6
votes2
answers3829
viewsTake AUTO_INCREMENT value from a table
I want to take the last record registered in a database table, ie the value of AUTO_INCREMENT from the table, I tried using: SELECT MAX(id) as max FROM people It works, but if I have no record in…
-
6
votes2
answers323
viewsShow command executed when SQL error occurs
Considering the following command executed BEGIN TRY -- Generate a divide-by-zero error. SELECT 1/0; END TRY BEGIN CATCH SELECT ERROR_NUMBER() AS ErrorNumber ,ERROR_SEVERITY() AS ErrorSeverity…
-
6
votes2
answers531
viewsMysql Auto increment even number
I have a Mysql database with a field id that is AUTO_INCREMENT. Has some configuration problem in it because when doing auto increment is coming out exactly in this sequence: ID: 2 ID: 12 ID: 22 ID:…
-
6
votes2
answers2918
viewsEntity Framework WITH (NOLOCK)
We can use the NOLOCK in the SELECT, thus avoiding the blockages with the commands of INSERT SELECT COUNT(Descricao) FROM Produtos WITH (NOLOCK) Is there any way to use the WITH(NOLOCK) in the…
-
6
votes4
answers10625
viewsConstructing SQL tables - use or not fields with Not Null?
I have a question regarding the construction of tables and field use Not Null. I know that for fields of Primary Key, it is necessary for him to be Not Null, but in other fields, what is the need to…
-
6
votes2
answers7324
viewsWhat is a lock and what are its causes?
I often come across the term: Table x is with lock. What it means and what its causes?
-
6
votes1
answer2140
viewsHow to remove spaces leaving only one?
Considering a variable with a text in SQL Server. How can I remove 2 or more spaces within the text? In this case leaving only 1 space. For example: '1 tigre, 2 tigres, 3 tigres' It should result…
-
6
votes1
answer1069
viewsDifference between temporary table and variable table type
In the Sqlserver there are temporary and variable table type resources. What would be the most advantageous or the best option to put in a Procedure which inserts the query that was returned from a…
-
6
votes1
answer2181
viewsIdentify if a string is capitalized in sql server!
I have a field in a table that must be filled all in lower case, but the user being user filled some lines in upper case. I want to identify what records are like this so I can request correction. I…
-
6
votes5
answers16830
viewsHow do I list all tables with their respective databases?
I would like to list all the tables in my database that ends with a specific name and also all my databases that match these tables. I tried to do it but nothing comes back. Query SELECT da.name AS…
-
6
votes1
answer61
viewsMysql function does not merge equals
I have this query for Mysql: SELECT email, COUNT(*) AS Vezes FROM automovel GROUP BY email HAVING COUNT(*) ORDER BY `data_cadastro` ASC It groups all the same emails together, and shows how many…
-
6
votes3
answers8795
viewsHow to round a number to the highest value in Mysql?
I was doing some tests on Mysql and then the need arose to have to round a number to the largest. It would be something like: if($numero == 1.5){ $numero = 2; } Even though I could do it like this,…
-
6
votes1
answer610
viewsWhat is the difference between REPLACE INTO or ON DUPLICATE KEY UPDATE
I am with the following doubt what is the difference between using REPLACE INTO or ON DUPLICATE KEY UPDATE in mysql, do not both serve the same purpose to make a change to a field or more in the…
-
6
votes2
answers1510
viewsHow to view the last 30 days of a query
I’m trying some alternatives to show only the records of the last 30 days, but I couldn’t make it work, the query I have is like this: SELECT comunidade.descricao AS nomeunidade ,comcargo.descricao…
-
6
votes1
answer1546
viewsHow to round up time?
I have the following function which treats an hour coming from the bank: SELECT f.chapa AS chapa, f.nome AS nome, f.secao AS cod_secao, f.nsecao AS desc_secao, c.codigo AS cod_funcao, c.nome AS…
-
6
votes2
answers1113
viewsHow to return the most appearing words in a column?
I have this table below which has two columns, being id and description: CREATE TABLE myBigTable ( id INT(11) AUTO_INCREMENT PRIMARY KEY, description TEXT NOT NULL ) After entering some records, I…
-
6
votes1
answer130
viewsDifference Between Using "FROM Table, Table2" X "Join Table ON Table2"
Considering the two forms of union of tables through a join(anyone): SELECT Tabela.A, Tabela.B, Tabela2.C FROM Tabela LEFT JOIN Tabela2 ON Tabela.Id = Tabela2.TabelaId And a union using from SELECT…
-
6
votes2
answers43536
viewsError doing Insert - String or Binary data > would be truncated
By making this Internet: INSERT INTO t_cmo_oit1980_leitura(id_oit_let,id_oit,rx_num, dt_rx) SELECT (1), (SELECT id_oit FROM t_cmo_oit1980 WHERE id_oit = 6574), (SELECT rx_num FROM…
-
6
votes4
answers829
viewsBest approach is to filter data in the database or application?
Suppose we have a table 'Vendas' with the following columns 'estado', 'cidade', 'vendedor', 'valor'. 'estado' and the 'cidade' would be where the sale was held and 'vendedor' would be what made this…